Heroku deployment secrets.toml

I’m trying to deploy my streamlit app to heroku because I can’t host the streamlit version on GitHub Pages to ad it to my website so I am now trying it on Heroku.

The app seems to be live on Heroku, but I am getting the following error:"

KeyError: 'st.secrets has no key "consumerKey". Did you forget to add it to secrets.toml or the app settings on Streamlit Cloud? More info: https://docs.streamlit.io/streamlit-cloud/get-started/deploy-an-app/connect-to-data-sources/secrets-management'
Traceback:
File "/app/.heroku/python/lib/python3.10/site-packages/streamlit/scriptrunner/script_runner.py", line 443, in _run_script
    exec(code, module.__dict__)
File "/app/main.py", line 127, in <module>
    positive, negative, neutral, polarity, tweet_list, positive_list, negative_list, neutral_list, neg, pos, neu, comp, keyword, noOfTweets = tf.twitter_get(user_word, num_of_tweets)
File "/app/twitterFunctions.py", line 83, in twitter_get
    consumerKey = st.secrets['consumerKey']
File "/app/.heroku/python/lib/python3.10/site-packages/streamlit/secrets.py", line 228, in __getitem__
    raise KeyError(_missing_key_error_message(key))```


In my setup.sh file, I have the following:

mkdir -p ~/.streamlit/

echo "\
[general]\n\
email = \"MY EMAIL ADDRESS HERE\"\n\
" > ~/.streamlit/credentials.toml
echo "\

[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
" > ~/.streamlit/config.toml

echo "\
[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
" > ~/.streamlit/secrets.toml

When I go to my secrets.toml file in .streamlit, I have all the required keys in it (and the streamlit version works, it’s just the heroku deployment that is showing the error):

consumerKey = 'THE KEY'

consumerSecret = 'THE KEY'

accessToken = 'THE KEY'

accessTokenSecret = 'THE KEY'```

It seems that the traceback is calling from the /app/.heroku directory but I can’t seem to find it, only the .streamlit path. I am assuming that might be the error but I don’t know how to fix it.

This writes over the secrets file with the config settings, which I assume isn’t what you actually want to do.

Thanks, I thought that it would maybe do that. However, when I have my setup.sh without those lines, I get the following error.:

Secrets file not found. Expected at: /app/.streamlit/secrets.toml

FileNotFoundError: [Errno 2] No such file or directory: '/app/.streamlit/secrets.toml'
Traceback:
File "/app/.heroku/python/lib/python3.10/site-packages/streamlit/scriptrunner/script_runner.py", line 443, in _run_script
    exec(code, module.__dict__)
File "/app/main.py", line 127, in <module>
    positive, negative, neutral, polarity, tweet_list, positive_list, negative_list, neutral_list, neg, pos, neu, comp, keyword, noOfTweets = tf.twitter_get(user_word, num_of_tweets)
File "/app/twitterFunctions.py", line 83, in twitter_get
    consumerKey = st.secrets['consumerKey']
File "/app/.heroku/python/lib/python3.10/site-packages/streamlit/secrets.py", line 222, in __getitem__
    value = self._parse(True)[key]
File "/app/.heroku/python/lib/python3.10/site-packages/streamlit/secrets.py", line 141, in _parse
    with open(self._file_path, encoding="utf-8") as f:

There definitely is a “secrets.toml” file in my /.streamlit folder and it works because the streamlit version of the app is up and running

Screenshot 2022-06-08 at 16.01.29

For heroku I just add the key value pairs in Settings → Config Vars from the dashboard:

In my streamlit app.py I don’t use st.secrets["private_gsheets_url"] to access the env vars/secrets. I instead use os.environ["private_gsheets_url"]

For nested env vars like gcp service account credentials, I first parse the json string as below:
parsed_credentials = json.loads(os.environ["gcp_service_account"])

credentials = Credentials.from_service_account_info(parsed_credentials,scopes=scopes)

Hope this helps.

3 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.