How to change text on st.text input already declared on top of the code

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.

login = st.text_input('Login', placeholder="Insert your login")
password = st.text_input('Password', placeholder="Insert your password", type="password")

Hey @Ueno_Hernan ,

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:

  1. Use st.session_state to handle the values of the input fields.

  2. 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:

  1. 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'].
  2. 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!

Cheers,
Nico

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