Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

simple streamlit app

  1. User

    simple streamlit app

  2. Assistant

    Sure! Here's an example of a simple Streamlit app:

    import streamlit as st
    
    def main():
        st.title("Simple Streamlit App")
        st.write("Welcome to my app!")
        
        # Demo of different widgets
        st.header("Demo Widgets")
        
        # Text input
        name = st.text_input("Enter your name")
        st.write(f"Hello, {name}!")
        
        # Number input
        age = st.number_input("Enter your age")
        st.write(f"You are {age} years old.")
        
        # Checkbox
        if st.checkbox("I agree to the terms"):
            st.write("Terms agreed!")
        
        # Button
        if st.button("Click me"):
            st.write("Button clicked!")
        
        # Selectbox
        color = st.selectbox("Select your favorite color", ["Red", "Green", "Blue"])
        st.write(f"Your favorite color is {color}.")
        
        # Slider
        value = st.slider("Select a value", 0, 100, 50)
        st.write(f"You selected {value}.")
        
        # Date input
        date = st.date_input("Select a date")
        st.write(f"You selected {date}.")
        
        # Time input
        time = st.time_input("Select a time")
        st.write(f"You selected {time}.")
        
    if __name__ == "__main__":
        main()
    

    To run this app, you'll need to have Streamlit installed. You can install it using pip:

    pip install streamlit
    

    Save the code to a file, e.g., app.py, and run the app by executing:

    streamlit run app.py
    

    You should see a local web page opened in your browser with the Simple Streamlit App.

  3. User

    anther one

  4. Assistant

    Sure! Here's another example of a simple Streamlit app that displays a plot:

    import streamlit as st
    import numpy as np
    import matplotlib.pyplot as plt
    
    def func(x):
        return np.sin(x)
    
    def main():
        st.title("Simple Streamlit App")
        st.write("Welcome to my app!")
        
        # Generate data
        x = np.linspace(0, 2*np.pi, 100)
        y = func(x)
        
        # Plot
        st.header("Plot")
        fig, ax = plt.subplots()
        ax.plot(x, y)
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        st.pyplot(fig)
        
    if __name__ == "__main__":
        main()
    

    In this example, we use Streamlit's pyplot function to display the plot generated by Matplotlib. You will need to have Matplotlib and NumPy installed to run this code. You can install them using pip:

    pip install matplotlib numpy
    

    Save the code to a file, e.g., plot_app.py, and run the app by executing:

    streamlit run plot_app.py
    

    You should see a local web page opened in your browser with the Simple Streamlit App displaying a plot.