Hi there, I’m trying to make a simple login page with the option to drag and drop files with th e keys “encoded” so the person won’t have to always fill the credentials.
So, I have the option to fill the inputs and I also wanna have the second option, and when I drop the files I want the text inputs for login and password to have the text filled with the credentials.
You can easily achieve this by using st.session_state to store and update the values of the login and password fields. When you drop the file, you can parse it and directly set the values of the text inputs by modifying the corresponding keys in st.session_state.
Here’s how you can implement this:
Use st.session_state to handle the values of the input fields.
When a file is dropped, extract the credentials and update st.session_state for those input fields.
Here’s an example:
import streamlit as st
# Function to handle the file upload and extract credentials
def handle_file_upload(uploaded_file):
if uploaded_file is not None:
# Assuming the file contains the credentials in plain text for this example
# e.g., "username:password"
content = uploaded_file.read().decode("utf-8")
login, password = content.split(":")
st.session_state['login'] = login
st.session_state['password'] = password
# Create a file uploader widget
uploaded_file = st.file_uploader("Upload your credentials file", type=["txt"])
# Handle the file upload
if uploaded_file:
handle_file_upload(uploaded_file)
# Create input widgets for login and password with session state keys
st.text_input("Login", key="login")
st.text_input("Password", key="password", type="password")
# Login button for demonstration
if st.button("Login"):
st.write(f"Logged in as {st.session_state['login']}")
Key Points:
Using key in text inputs: By assigning a key (e.g., "login" and "password"), the values entered in those fields are automatically stored in st.session_state['login'] and st.session_state['password'].
Setting st.session_state directly: When the file is uploaded and credentials are parsed, you set the corresponding values in st.session_state, which will immediately update the input fields.
This way, after dropping the file, the input fields for the login and password will automatically be filled with the extracted credentials, and users won’t need to manually input them every time.
Let me know if this works or if you need more help!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.