Good morning,
While testing locally my project(testing) using subprocess function to open/ run another page('run streamlit.page.py), it is working correctly. But when deploying it on streamlit, it not taking any action when clicking on the button.
Another test i did at deployment stage, trying to create as if a login page before accessing the analytical page, After inserting the credentials, when clicking on the log in button, not action is being taken. When it worked perfectly on localhost.
Can someone help me please
Hi @Kasarajen_Mootien and welcome to our community!
Could you please try to bring all your code under one roof by ditching the subprocess
module and pulling the code from your other script into your main Streamlit app?
Thanks,
Charly
Hi @Charly_Wargnier, thank you for your fast response.
yes sure it is working as if a sidebar. But is it possible to have as if a login before the user can access the page please?
Kind regards,
Kasarajen
1 Like
Thanks @Kasarajen_Mootien for the quick feedback!
So while Streamlit does not provide built-in support for user authentication, you can still create a simple login interface within your app. Here’s a very basic way of doing this:
Here’s the code:
import streamlit as st
def main():
st.write("Welcome to the main application") # This is the main application code
def check_password(stored_password, user_password):
if stored_password == user_password:
return True
else:
return False
def password_input():
st.subheader("Login to the application")
username = st.text_input("User Name")
password = st.text_input("Password", type='password')
return username, password
def login():
username, password = password_input()
if st.button("Login"):
stored_password = "your_password" # You can encrypt this password or store it securely somewhere
if check_password(stored_password, password):
st.write(f"Logged in as {username}")
main()
else:
st.error("The password you entered is incorrect")
login()
Is it what you are after?
Thanks,
Charly
Thank you.
At testing stage all works fine.
But i issue is becoming when i deploy it; after clicking on the log in button no action is been taken to redirect me toward the main page. So, the issue is the load of new page (run of another py code). Else the other time would be working fine.
Can you help please? Like a sample would have help; as with a log in that will redirect to a page that have multiple pages.
Thank you a lot again
Kind regards,
Kasarajen
1 Like
Thanks for the mock-ups and feedback!
It’s a bit difficult to diagnose and advise without any benchmarked code, but as an alternative, here’s a simple function I created for my internal needs to apply a login to every single page of an MPA app.
import streamlit as st
# Hardcoded password
correct_password = "one"
def check_password():
"""Returns `True` if the user has the correct password."""
# Initialize 'password_correct' if it's not in the session state
if "password_correct" not in st.session_state:
st.session_state["password_correct"] = False
# If the password has already been entered correctly, don't ask again
if st.session_state.password_correct:
return True
# Initialize 'password' if it's not in the session state
if "password" not in st.session_state:
st.session_state["password"] = ""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if st.session_state.password == correct_password:
st.session_state.password_correct = True
else:
st.session_state.password_correct = False
# If the password is not correct, show input + error.
st.text_input(
"Please authenticate to access the pages",
type="password",
on_change=password_entered,
key="password",
)
if st.session_state.password_correct:
return True
elif st.session_state.password:
st.error("😕 Password incorrect")
return False
You would then call this on every page, this way, at the top of each page.
from password import check_password
if check_password():
Would that work as expected?
Let me know.
Thanks,
Charly