How to use an entire json file in the 'secrets' app settings when deploying on the community cloud?

Hello,
I may have a question on how to deploy an app on the community server that requires a firebase user authentication, in relation to passing API Keys, etc. through secrets.toml.

I really like this feature, it’s super useful.
For a firebase authentication, one needs to do the following, i.e pass an entire json file:

cred = firebase_admin.credentials.Certificate("my_project_settings.json")

However, as far as I know, we can only use variables in the settings during the app deployment.

What I’m doing for now is to copy all the fields needed in my_project_settings.json in the secrets.toml file then make a dictionary from these variables as input for the Certificate method.

It’s working but I’m wondering if there’s a better way to do that.

Thanks in advance.

Regards,
Jonathan

ps: I’ve been using streamlit since only a month now and I really like it.

Hi there!
Streamlit requires secrets to be in the TOML format. This does not allow the normal usage of JSON files as a dictionary. However, in order to directly create a dictionary, you can format your JSON data to be TOML compatible. In case of firebase credentials, it can be something like this:

[firebase]
my_project_settings = {  "type" = "fsd",   "project_id"= "fds",   "private_key_id"= "sdf ",   "private_key"= "asfd",   "client_email"= "dsf",   "client_id"= "dsf",   "auth_uri"= "fsd",   "token_uri"= "sfd",   "auth_provider_x509_cert_url"= "sfd",   "client_x509_cert_url"= "sdf"}

As far as I know, it has to be in a single line and use = to separate key-value pairs instead of :.
After copying your JSON, doing a find and replace for the following should work:

  1. Find ,\n, replace all with ,
  2. Find : replace all with =

You can then access it in your project directly as a dictionary.

import streamlit as st

fb_credentials = st.secrets["firebase"]['my_project_settings'] // returns a dictionary

Here’s a great discussion about dictionary variables in the secrets file:

Cheers,
Moiz

thanks @momos

When I used your solution, the fb_credentials type was actually casted as <class 'streamlit.runtime.secrets.AttrDict'>, not a python dict as firebase expects.

so I simply re-casted it as a dictionary and it worked fine now.

Thanks for the help.

1 Like

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