Having trouble passing secrets to connection for Snowflake

I’m trying to connect a Snowflake account with st.connect and the Streamlit secrets manager, but I cannot figure out how to pass all the required parameters to create the connection. The main issue seems to be with setting the connection_name for Snowflake.

Have tried variations from:

  • st.connect("snowflake") doesn’t seem to find secrets
  • st.connect("snowflake", **st.secrets["snowflake"]) complains about missing name` param
  • st.connect("snowflake_connection", type="snowflake", **st.secrets["snowflake"]) added connection_name to secrets manager, but still does not get recognized

App is deployed in Cloud: https://performance-expectations.streamlit.app/
Github repo: GitHub - risafangs/performance-expectations

Error:

2023-11-20 05:45:23.727 Uncaught app exception

Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 264, in _get_or_create_cached_value

    cached_result = cache.read_result(value_key)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_resource_api.py", line 500, in read_result

    raise CacheKeyNotFoundError()

streamlit.runtime.caching.cache_errors.CacheKeyNotFoundError


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 312, in _handle_cache_miss

    cached_result = cache.read_result(value_key)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_resource_api.py", line 500, in read_result

    raise CacheKeyNotFoundError()

streamlit.runtime.caching.cache_errors.CacheKeyNotFoundError


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script

    exec(code, module.__dict__)

  File "/mount/src/performance-expectations/main.py", line 19, in <module>

    conn = st.connection("snowflake", type="snowflake", **st.secrets["snowflake"])

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/connection_factory.py", line 312, in connection_factory

    conn = _create_connection(

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/metrics_util.py", line 396, in wrapped_func

    result = non_optional_func(*args, **kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/connection_factory.py", line 94, in _create_connection

    return __create_connection(name, connection_class, **kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 212, in wrapper

    return cached_func(*args, **kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 241, in __call__

    return self._get_or_create_cached_value(args, kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 267, in _get_or_create_cached_value

    return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 321, in _handle_cache_miss

    computed_value = self._info.func(*func_args, **func_kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/connection_factory.py", line 87, in __create_connection

    return connection_class(connection_name=name, **kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/connections/base_connection.py", line 71, in __init__

    self._raw_instance: Optional[RawConnectionT] = self._connect(**kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/connections/snowflake_connection.py", line 115, in _connect

    raise e

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/connections/snowflake_connection.py", line 79, in _connect

    return snowflake.connector.connect(**conn_kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/snowflake/connector/__init__.py", line 54, in Connect

    return SnowflakeConnection(**kwargs)

  File "/home/adminuser/venv/lib/python3.9/site-packages/snowflake/connector/connection.py", line 380, in __init__

    raise Error(

snowflake.connector.errors.Error: Invalid connection_name 'snowflake', known ones are []

[05:45:25] 🔄 Updated app!

[05:47:06] 🐙 Pulling code changes from Github...

2023-11-20 05:47:07.741 Uncaught app exception

Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script

    exec(code, module.__dict__)

  File "/mount/src/performance-expectations/main.py", line 19, in <module>

    conn = st.connection(type="snowflake", **st.secrets["snowflake"])

TypeError: connection_factory() missing 1 required positional argument: 'name'

[05:47:07] 📦 Processing dependencies...

[05:47:07] 📦 Processed dependencies!

2023-11-20 05:47:07.747 Uncaught app exception

Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script

    exec(code, module.__dict__)

  File "/mount/src/performance-expectations/main.py", line 19, in <module>

    conn = st.connection(type="snowflake", **st.secrets["snowflake"])

TypeError: connection_factory() missing 1 required positional argument: 'name

Versions:

  • Streamlit 1.28.2
  • Python 3.9

Appreciate any ideas/examples of how to properly create the Snowflake connection. Thanks.

Hi @risafangs

Could you also share what was specified in the secrets (please black out the actual username and password)?

st.secrets["snowflake"] would allow you to retrieve a variable specified in TOML format from the Secrets stored on Community Cloud as in

snowflake="123"

To retrieve both username and password in as you had when specifying with ** in **st.secrets["snowflake"]

You would specify the following in your secrets:

[snowflake_credentials]
username = "my_username"
password = "my_password"

Then you can call via **st.secrets["snowflake_credentials"] (please see Docs for more detail: Secrets management - Streamlit Docs)

Hope this helps!

Thanks @dataprofessor! This is what I have in Streamlit’s Secrets section (values changed):

[connections.snowflake]
connection_name = "snowflake"

[snowflake]
user = "my_username"
account = "account_locator"
authenticator = "externalbrowser"
warehouse = "wh_name" 
database = "dev_db" 
schema = "development_schema"
role = "role_name"

I tried renaming the second block [snowflake] to [snowflake_credentials] and adding that to **st.secrets[] but it cannot find them and still wants name:

KeyError: 'snowflake_credentials'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script

    exec(code, module.__dict__)

  File "/mount/src/performance-expectations/main.py", line 19, in <module>

    conn = st.connection(**st.secrets["snowflake_credentials"])

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/secrets.py", line 311, in __getitem__

    raise KeyError(_missing_key_error_message(key))

KeyError: 'st.secrets has no key "snowflake_credentials". 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'

[17:11:23] 📦 Processing dependencies...

[17:11:23] 📦 Processed dependencies!

[17:11:25] 🔄 Updated app!

2023-11-20 17:11:43.033 Uncaught app exception

Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script

    exec(code, module.__dict__)

  File "/mount/src/performance-expectations/main.py", line 19, in <module>

    conn = st.connection(**st.secrets["snowflake_credentials"])

TypeError: connection_factory() missing 1 required positional argument: 'name'

Hi @risafangs

It seems that you’ve split your secrets into 2 sections. Could you try following this tutorial and structuring the secrets to those mentioned in the tutorial: