Streamlit-Authenticator, Part 1: Adding an authentication component to your app

st.button is not working, means user credential is not save.

1 Like

Below code is working fine.
I have used st.expander instead of st.button

    with st.expander("Register New User"):
        try:
            if authenticator.register_user("Register user", preauthorization=False):
                st.success("User registered successfully")
        except Exception as e:
            st.error(e)

        with open("config.yaml", "w") as file:
            yaml.dump(config, file, default_flow_style=False)
2 Likes

@mkhorasani Please give me reply if possible.

2 Likes

Yes, I wanted to mention the same thing, using an st.button will not work since the register user widget will only be invoked if the button is pressed, which means that every time Streamlit re-runs the page you will lose the widget. st.expander is probably the best option.

2 Likes

@mkhorasani

Traceback (most recent call last):
  File "C:\Users\dell\.virtualenvs\SLAB-Webapp-A22S0KMh\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\dell\Desktop\code\Testing Temp\SLAB-Webapp\SLAB\pages\1_Add_Base_Files.py", line 103, in <module>
    authenticator.logout("Logout", "sidebar")
  File "C:\Users\dell\.virtualenvs\SLAB-Webapp-A22S0KMh\lib\site-packages\streamlit_authenticator\authenticate.py", line 217, in logout
    if st.sidebar.button(button_name):
  File "C:\Users\dell\.virtualenvs\SLAB-Webapp-A22S0KMh\lib\site-packages\streamlit\runtime\metrics_util.py", line 332, in wrapped_func
    result = non_optional_func(*args, **kwargs)
  File "C:\Users\dell\.virtualenvs\SLAB-Webapp-A22S0KMh\lib\site-packages\streamlit\elements\button.py", line 155, in button
    return self.dg._button(
  File "C:\Users\dell\.virtualenvs\SLAB-Webapp-A22S0KMh\lib\site-packages\streamlit\elements\button.py", line 429, in _button
    button_state = register_widget(
  File "C:\Users\dell\.virtualenvs\SLAB-Webapp-A22S0KMh\lib\site-packages\streamlit\runtime\state\widgets.py", line 163, in register_widget
    return register_widget_from_metadata(metadata, ctx, widget_func_name, element_type)
  File "C:\Users\dell\.virtualenvs\SLAB-Webapp-A22S0KMh\lib\site-packages\streamlit\runtime\state\widgets.py", line 208, in register_widget_from_metadata
    raise DuplicateWidgetID(
streamlit.errors.DuplicateWidgetID: There are multiple identical `st.button` widgets with the
same generated key.

When a widget is created, it's assigned an internal key based on
its structure. Multiple widgets with an identical structure will
result in the same internal key, which causes this error.

To fix this error, please pass a unique `key` argument to
`st.button`

Can you tell me how I can fix this bug?

1 Like

I think this is the Internal Error for same key for multiple st.button.

1 Like

Yes, by the looks of it you are using multiple buttons with the exact same name. To avoid this, please pass a unique key to each one using the ‘key’ parameter.

1 Like

Yes, But this buttons are used in streamlit-authenticator code, Then how can I pass from my side?

1 Like

Are you using streamlit-authenticator with a multi-page app that has several logout buttons? If so, then yes this bug will appear, I will release a new version that will solve this issue soon. In the meantime, you can modify the source code for the package to rectify this.

2 Likes

Yes, I have use streamlit-authenticator in multipage app.

1 Like

As soon as I’ve pushed an update to the package I will let you know.

1 Like

import pandas as pd
import openai
import os
import streamlit as st
from dotenv import load_dotenv
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
import plotly.express as px
import pickle
from pathlib import Path
import streamlit_authenticator as stauth
import webbrowser
import requests
import yaml
from yaml.loader import SafeLoader

with open(‘./file.yaml’) as file:
config = yaml.load(file, Loader=SafeLoader)

authenticator = Authenticate(
config[‘credentials’],
config[‘cookie’][‘name’],
config[‘cookie’][‘key’],
config[‘cookie’][‘expiry_days’],
config[‘preauthorized’]
)

Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 552, in _run_script
exec(code, module.dict)
File “/Users/userman/Documents/march_venv/strava_gradio/Home.py”, line 21, in
authenticator = Authenticate(
^^^^^^^^^^^^
NameError: name ‘Authenticate’ is not defined

Am I missing something obvious here?

Was following your tutorial

2 Likes

Hi Mohammad,
Many thanks for the Login App. It works great when deployed locally, but deployment on the StreamlitCloud seems to be a bit more of a complex issue.
Where to store the user data that originally was saved in the yaml file? Do you use st.secrets to manage this? If so - could we see how? I have been struggling to the deploy this on the Cloud for quite a while now and I am under impression that this app makes sense only in the online environment.
Thanks in advance!

1 Like

facing the same issue.

1 Like

Hi Mohammad, hi everybody

is there a way to allow only one certain domain as part of the registration when registering a new user?

Thanks in advance

1 Like

I used:

authenticator = stauth.Authenticate( config['credentials'], config['cookie']['name'], config['cookie']['key'], config['cookie']['expiry_days'], config['preauthorized'] )

1 Like

Hello,

It’s possible to set/change labels of forms (login, register, …)?

1 Like

Your Streamlit module is quite facilitative and effective. However, I encountered a problem. Let’s say you want to display the st.session_state[“name”] property on another page when creating a multi-page application. If only one user is online, you don’t face any issues while allowing the user to navigate through other pages. You print the name correctly. However, when a second user logs into the system, the other user cannot visit different pages. You encounter an error like this: “KeyError: 'st.session_state has no key “name”. Did you forget to initialize it?” Am I using it incorrectly? If so, could you create a tutorial for a multi-page and multi-user scenario?

2 Likes

I deployed my Streamlit app after adding the authenticator. Multiple users were created there using the Register feature, however now I need to make some changes to the app. How can I maintain the users created on the Streamlit deployed app, as pushing the new code will update the config file?

1 Like

You may save the config in data sources.

1 Like