Here is how to build Streamlit apps in Python.


  1. Building a Streamlit app is a straightforward and enjoyable way to create interactive and beautiful web applications using Python. This tutorial will guide you through the basics of Streamlit, showing you how to build a simple yet functional app from scratch.

    Step 1: Setting Up Your Environment

    Before you start, ensure you have Python installed on your computer. Streamlit requires Python 3.6 or later.

    1. - Install Streamlit: Open your terminal (or Command Prompt on Windows) and install Streamlit using pip.

      pip install streamlit


      - Create a Python File:

    2. Create a new Python file for your app, e.g., my_app.py.

    Step 2: Writing Your First Streamlit App

    Open your Python file in a text editor or IDE, and let's start coding.

    1. - Import Streamlit:

      import streamlit as st


    2. - Add a Title:

      Streamlit has built-in functions to add elements to your app, such as text, data, plots, and more. Let's start by adding a title.

      st.title('My First Streamlit App')


    3. - Add Some Text:

      Below the title, add a brief introduction to your app using the write function.

      st.write('This is a simple Streamlit app that does nothing much yet!')

    4. - User Input:

      Let's make the app interactive by allowing users to enter their name.

      name = st.text_input('Enter your name:')


    5. - Display a Greeting:

      Use the input to display a personalized greeting.

      if name:
      st.write(f'Hello, {name}!')

    Step 3: Running Your App

    1. - Start Your App: Open a terminal, navigate to the folder containing your Python file, and run:

      streamlit run my_app.py


    2. - View Your App: Your default web browser should automatically open and display your Streamlit app. If it doesn't, the terminal will provide a local URL that you can use to view your app.

    Step 4: Expanding Your App

    Streamlit offers a wide range of widgets and functions to add more interactivity and data to your app. Here are a few ideas:

    • - Display a Button:

      if st.button('Say hello'):
      st.write('Hello, world!')

    • - Create a Slider:

      Use a slider for numerical inputs.

      age = st.slider('How old are you?', 0, 130, 25)
      st.write("I'm ", age, 'years old')


    • - Plotting Data:

      Streamlit can integrate with libraries like Matplotlib or Plotly to display data visually.

      import numpy as np
      import matplotlib.pyplot as plt data = np.random.randn(100)
      plt.hist(data, bins=20) st.pyplot(plt)

    Step 5: Sharing Your App

    Once you're happy with your app, you might want to share it with others. Streamlit Sharing, Heroku, or similar PaaS (Platform as a Service) can host your Streamlit apps. The easiest one is Streamlit Sharing. You will find a "Deploy" button on your web app page to publish the your app on the internet through a public URL.

    Conclusion

    Streamlit makes it incredibly simple to build and deploy interactive web apps using Python. By combining Python's power with Streamlit's simplicity, you can quickly create apps for data analysis, machine learning, or just for fun.

Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.