-
Notifications
You must be signed in to change notification settings - Fork 4k
Set auto_envvar_prefix to STREAMLIT #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Awesome! I'm having trouble testing this, though. How do I set the The problem I'm running into is that |
|
I'm looking into this. |
8713b58 to
2f7c9e0
Compare
Set auto_envvar_prefix to STREAMLIT to accept parameters from environment variables in addition to regular CLI arguments. This is a Click option that will automatically get values from env vars if available.
2f7c9e0 to
1ed7204
Compare
|
@tvst I've moved The format of the environment variables that Click generates is PREFIX_COMMAND_VARIABLE. So, for the If you temporarily set I noticed that you attempted to set With the prefix on run_main instead of the root main: |
|
Another option is to leave This overlaps with the Another thing that might be useful is assigning an envvar to the |
Override the environment variable names for the options dynamically
added by configurator_options to be STREAMLIT_CONFIG_{option}. So, for
--server.port, the corresponding envvar is STREAMLIT_CONFIG_SERVER_PORT
instead of STREAMLIT_RUN_SERVER_PORT.
Set the environment variable for the file_or_url argument on the run command to STREAMLIT_RUN_FILE_OR_URL. Click does not do automatic envvars for arguments. This makes it possible to specify the file or url to run using an environment variable.
|
I've pushed commits to override the envvar names for the config options and add an envvar for the |
|
Awesome PR @kinghuang! Two questions (@tvst should be the decider):
$ export STREAMLIT_RUN_TARGET=sa/gui.py
$ streamlit run ${STREAMLIT_RUN_TARGET} |
tvst
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried this and it's really amazing! 💯
Can you also add this to docs/cli.md line 63:
Shows all config options available for Streamlit, including their current
values. You can set these options in four different ways:
- **In a global config file at `~/.streamlit/config.toml`.** For instance:
```toml
[server]
port = 80
```
- **In a per-project config file at `$CWD/.streamlit/config.toml`,** where
`$CWD` is the folder you're running Streamlit from.
- **Through `STREAMLIT_CONFIG_*` environment variables,** such as:
```bash
$ export STREAMLIT_CONFIG_SERVER_PORT=80
```
- **As flags in the command line** when running `streamlit run`. For example:
```bash
$ streamlit run your_script.py --server.port 80
```
And while you're at it, do you mind fixing line 26? 😉
.. note::
When passing your script some custom arguments, **they must be passed after a
"--"** (double dash). Otherwise the arguments get interpreted as arguments to
Streamlit itself.
lib/streamlit/cli.py
Outdated
| parsed_parameter["param"], | ||
| help=parsed_parameter["description"], | ||
| type=parsed_parameter["type"], | ||
| envvar='STREAMLIT_CONFIG_{}'.format(parsed_parameter["param"].upper()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the suffix use snake case too. We have a function called to_snake_case() in case_converters.py that should do the trick.
In which case this line will get really long and nasty, so you could pull this logic into the _convert_config_option_to_click_option function.
Even better if we could change line 145 to just
config_option = click.option(**parsed_parameter)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll take a look at all these tomorrow!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved the logic into _convert_config_option_to_click_option(). But, I'm not so sure about applying to_snake_case.
For an option like server.baseUrlPath, we have:
STREAMLIT_CONFIG_SERVER_BASE_URL_PATH (with snakecase)
STREAMLIT_CONFIG_SERVER_BASEURLPATH (without snakecase)
I think the without snakecase version matches the underlying config name better. With the snakecase version, I wouldn't know if it's supposed to match a config named server.base.url.path, server.baseUrl.path, serverBaseUrlPath, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plan is to have only a two-level hierarchy for config options, so there shouldn't be any ambiguity so I think STREAMLIT_CONFIG_SERVER_BASE_URL_PATH is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The non-snakecase version has already been merged. But, here's how some other projects do config to environment variable mappings, for comparison. Overall, I feel that converting camelCase to straight uppercase without snake-casing is the better choice because it matches more closely with other apps.
Click's Default Behaviour
Turning on automatic environment variables in Click converts option names to uppercase, without snake casing.
--server.baseUrlPath == {PREFIX}_SERVER_BASEURLPATH
Traefik
Traefik's configs have camelCase names, and are mapped to CLI options in lowercase. The corresponding environment variables are not snake-cased.
[accessLog] filePath == --accesslog.filepath == TRAEFIK_ACCESSLOG_FILEPATH
Kafka (using Confluent Platform images)
Kafka's configs are all lowercase, and tend to have each word separated by a period (as opposed to camelCasing or snake_casing). In the Confluent Platform image for Kafka, the corresponding environment variables are only uppercased.
auto.create.topics.enable == KAFKA_AUTO_CREATE_TOPICS_ENABLE
Grafana
Grafana's configs are snake cased, which are preserved in environment variables.
[server] http_port == GF_SERVER_HTTP_PORT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops, I think I merged this too soon. No problem, we'll address it in another PR.
|
For @treuille's questions:
|
Move the logic for constructing an environment variable name out of configuration_options() to _convert_config_option_to_click_option(), where the other Click options are prepped.
Add the copy from @tvst explaining the now four ways Streamlit can be configured.
Add the updated copy from @tvst about delineating custom script arguments with --.
👍 |
|
Ok, I just changed it to |
|
Awesome. I couldn't quite squeeze in that change in between meetings, today. :D |
Ah. That's compelling. Still I feel like |
|
Btw, this is really exciting work, @kinghuang ! |
* develop: Use custom context manager for temporary file in cli.py (streamlit#489) Update pull_request_template.md Update pull_request_template.md Handle lack of trailing slash (streamlit#528) Set auto_envvar_prefix to STREAMLIT (streamlit#477) Strip slashes in path components (streamlit#523) Create doc_improvement.md ESlint: allow @ts-ignore (streamlit#499) Update Pipfile (streamlit#505) Release 0.49.0 (streamlit#509) Removing package json (streamlit#501) Feature/input number (streamlit#416) ESLint warnings are fatal in Circle (streamlit#492) Doc updates (streamlit#286)
|
Thanks for this PR @kinghuang , it's exactly what I'm after but I'm just a bit confused on the correct usage. The v0.50.1 release notes state:
But the docs state:
I've tried setting both |
|
@dijitali Ah, the docs are incorrect! The For the This is also shown in the help text for the run command. |
|
Perfect, many thanks. Wasn't aware the |
Issue: #476
Description: Set auto_envvar_prefix to STREAMLIT to accept parameters from environment variables in addition to regular CLI arguments. This is a Click option that will automatically get values from env vars if available.
Contribution License Agreement
By submiting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.