Getting error in self._secrets in using st.experimental_connection for duckdb

I am using st.experimental_connection for duckdb and for that I am using ExperimentalBaseConnection Base Class.

class DuckDBConnection(ExperimentalBaseConnection[duckdb.DuckDBPyConnection]):

    def _connect(self, **kwargs) -> duckdb.DuckDBPyConnection:
        if 'database' in kwargs:
            db = kwargs.pop('database')
        else:
            st.write(self._secrets)
            db = self._secrets['database']

        return duckdb.connect(database=db, **kwargs)

and calling the st.experimental_connection

conn = st.experimental_connection("duckdb", type=DuckDBConnection, database=':memory:')
conn

But while printing the secrets , I am getting a empty dict , though it was throwing the error to me

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

I had already added database as key in secrets.toml file

do you mind sharing your secrets.toml format masking the credentials?

secrets.toml format

database = "*****"
username = "****"
password = "****"

Hi @Harsh_Gupta sorry about the late follow up. If this is for your app locally, I’d check if your secret.toml is in the right directory

- streamlit_app.py
- .streamlit
   - secrets.toml

You can write out the secret st.write(st.secrets) to check
if this your app in cloud, you would need to manually add secrets to the secret managements under settings. Let me know if this helps.

st.write(st.secrets) is working , but after inheriting ExperimentalBaseConnection , I am implementing the connect method , in which I will be using secrets for making the connection , there it is not working …

def _connect(self, **kwargs) -> duckdb.DuckDBPyConnection:
        st.write("check",st.secrets)
        if 'database' in kwargs:
            db = kwargs.pop('database')
        else:
            db = self._secrets['database']

        return duckdb.connect(database=db, **kwargs)

it is coming as empty only

it looks like it is picking up the database in your kwarg. if you want to read the credentials from the secrets, could give a try on and see it that returns anything?
conn = st.experimental_connection("duckdb", type=DuckDBConnection)

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