How to assign a dictionary to a variable in secrets.toml?

Summary

My intention is to copy and paste a dictionary with JSON credentials but to simplify the problem I don’t know how to assign dictionaries to a variable in secrets.toml if it is possible.

Steps to reproduce

Code snippet:


add code here

.streamlit/secrets.toml

this_var = {‘a key’: ‘a value’}

Expected behavior:
You should simply assign a dictionary for use in the app.

Actual behavior:
The system displays a parse error.

Error parsing Secrets file.
TomlDecodeError: Invalid inline table encountered (line 3 column 1 char 27)

Debug info

  • Streamlit version: (1.14.0)
  • Python version: (3.10.6)
  • Using venv with pip
  • OS version: Ubuntu 22.04
  • Browser version: Brave Versión 1.45.118 but Firefox 106.0.5 shows the same error.

Requirements file

pandas
google_api_python_client
gsheetsdb
streamlit

Additional information

The application I ran locally.

This is not valid toml, which is what secrets.toml expects. The equivalent to that in toml is

[this_var]
'a key' = 'a value'

Or like this

this_var = { 'a key' = 'a value' }

Either one of those means this will output

{'a key': 'a value'}

when you run this code

st.write(st.secrets["this_var"])
1 Like

Thanks for responding so quickly. If you define a section with [this_var] it does work but the Python dictionary-like option with

this_var = { ‘a key’ = ‘a value’ }

shows the following error.

KeyError: ‘st.secrets has no key “type”. Did you forget to add it to secrets.toml or the app settings on Streamlit Cloud? More info: Secrets management - Streamlit Docs

Are you sure you didn’t put type = { ..something...} in your secrets.toml?

Here’s my secrets.toml,

this_var = { 'a key' = 'a value' }

[this_var2]
'a key' = 'a value'

and my streamlit app, and it works well.

import streamlit as st

st.write(st.secrets["this_var"])
st.write(st.secrets["this_var2"])

You are right. I have created a new app just with the code you have written and it works correctly.

Doing some more tests I found the error. If the entry line in secrets.toml of the dictionary has more than one line it causes the error. That is to say,

# ./streamlit/secrets.toml
this_var1 = { 'key 1' = 'value 1', 'key 2' = 'value 2' }
this_var2 = {
  'key 1' = 'value 1',
  'key 2' = 'value 2' 
}

this_var2 produces the error. Of course, having the assignments delimited by braces {} to define the dictionary structure I understood that the line endings were ignored, but it seems that this is not the case.

I appreciate your response.

1 Like

You’re right, that’s not valid toml: TOML: English v1.0.0

Inline tables are intended to appear on a single line. A terminating comma (also called trailing comma) is not permitted after the last key/value pair in an inline table. No newlines are allowed between the curly braces unless they are valid within a value. Even so, it is strongly discouraged to break an inline table onto multiples lines.

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