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

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

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)
1 Like

@mkhorasani Please give me reply if possible.

1 Like

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.

1 Like

@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?

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

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.

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

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.

1 Like

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

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

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

1 Like

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!

facing the same issue.

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

I used:

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