Skip to content

Commit 52a3ab2

Browse files
Pierrciosanseviero
andauthored
Tweaks to env vars in Spaces (#1284)
* Tweaks to env vars in Spaces I think this is clearer, and it will also prevent Python noobs like me from having to google how to read an env var! * Update docs/hub/spaces-overview.md * Apply suggestions from code review Co-authored-by: Omar Sanseviero <[email protected]> --------- Co-authored-by: Omar Sanseviero <[email protected]>
1 parent 5ef8144 commit 52a3ab2

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

docs/hub/spaces-overview.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,25 @@ If your app requires environment variables (for instance, secret keys or tokens)
6666
<img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/secrets-and-variables-dark.png"/>
6767
</div>
6868

69+
You can use:
6970

70-
Variables are publicly accessible and viewable and will be automatically added to Spaces duplicated from your repository. They are exposed to your app as environment variables.
71+
* **Variables** if you need to store non-sensitive configuration values. They are publicly accessible and viewable and will be automatically added to Spaces duplicated from yours.
72+
* **Secrets** to store access tokens, API keys, or any sensitive values or credentials. They are private and their value cannot be read from the Space's settings page once set. They won't be added to Spaces duplicated from your repository.
7173

72-
For Static Spaces, they are available through client-side JavaScript in `window.huggingface.variables`.
7374

74-
For Docker Spaces, check out [environment management with Docker](./spaces-sdks-docker#secrets-and-variables-management).
75+
Accessing secrets and variables is different depending on your Space SDK:
7576

76-
Secrets are private and their value cannot be retrieved once set. They won't be added to Spaces duplicated from your repository. The secrets will be exposed to your app with [Streamlit Secrets Management](https://blog.streamlit.io/secrets-in-sharing-apps/) if you use Streamlit, and as environment variables in other cases. For Docker Spaces, please check out [environment management with Docker](./spaces-sdks-docker#secrets-and-variables-management). Users are warned when our `Spaces Secrets Scanner` [finds hard-coded secrets](./security-secrets).
77+
- For Static Spaces, both are available through client-side JavaScript in `window.huggingface.variables`
78+
- For Docker Spaces, check out [environment management with Docker](./spaces-sdks-docker#secrets-and-variables-management)
79+
- For Streamlit Spaces, secrets are exposed to your app through [Streamlit Secrets Management](https://blog.streamlit.io/secrets-in-sharing-apps/), and public variables are directly available as environment variables
80+
81+
For other Spaces, both are exposed to your app as environment variables. Here is a very simple example of accessing the previously declared `MODEL_REPO_ID` variable in Python (it would be the same for secrets):
82+
```py
83+
import os
84+
print(os.getenv['MODEL_REPO_ID'])
85+
```
86+
87+
Spaces owners are warned when our `Spaces Secrets Scanner` [finds hard-coded secrets](./security-secrets).
7788

7889
## Duplicating a Space
7990

0 commit comments

Comments
 (0)