Execution of Code after user presses a button and inputs an URL

Hello,

i want to build a small app with streamlit to evaluate privacy metrics on health data.

Firstly, the user should be asked whether he wants to upload health data from a server or from his local device.

So i implemented two buttons. If user presses the “upload from server” button, he should be able to enter the server url. After pressing Enter on the keyboard some code should be executed and the user should see a success message. I cant get that working with streamlit.

After entering an URL (Screenshot 1) i just see the “start page” again, no success message (Screenshot 2).

This is my code:

import streamlit as st
import request_data

def main():
    st.title("Privacy Analysis of FHIR Data")
    st.header("Welcome. To start, select to load data from HAPI FHIR server or to upload FHIR Data in JSON format from your device")

    if st.button("Load Data from Server"):
        # Get server URL by user input
        url_input = st.text_input(
            'Upload FHIR Data from HAPI server',
            placeholder='Enter server URL here',
            help='(e.g. www.fhir.com/test)'
            )

        if url_input:
            # Show success message after pressing Enter
            st.success("URL erfolgreich eingegeben")
            #process data
            data = request_data.request_data(url_input)
            st.write(data)

    elif st.button("Load Data from Computer"):
        st.write("tbd")

Im pretty sure im just misunderstanding the workflow mechanics in Streamlit, dont i?

Thank you in advance for your help.

Buttons don’t retain state. If you nest a text input inside a button, then the text input will disappear as soon as the user interacts with it because the app will rerun and the button will be false. In your case above, this means the interior of if url_input: will never execute.

Check out this guide on Button behavior. It covers this point and provides various patterns to work with buttons.

1 Like

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