Creating Your First Streamlit App
Key Concepts
To create your first Streamlit app, you need to understand the following key concepts:
- Streamlit Basics: Understanding how Streamlit works and its core components.
- Installation: Setting up your environment to run Streamlit.
- Basic Structure: The fundamental structure of a Streamlit app.
- Running the App: How to execute your Streamlit app.
Streamlit Basics
Streamlit is an open-source app framework that allows you to create web applications for data science and machine learning in Python. It simplifies the process of creating interactive web apps by allowing you to write Python scripts that automatically generate web interfaces.
Installation
Before you can create a Streamlit app, you need to install the Streamlit library. You can do this using pip, the Python package installer. Run the following command in your terminal:
pip install streamlit
Basic Structure
A basic Streamlit app consists of a Python script that imports the Streamlit library and uses its functions to create the app's interface. Here is a simple example:
import streamlit as st st.title("My First Streamlit App") st.write("Hello, World!")
In this example, st.title
sets the title of the app, and st.write
displays the text "Hello, World!" on the app's interface.
Running the App
Once you have written your Streamlit app, you can run it using the Streamlit command-line interface. Navigate to the directory where your script is located and run the following command:
streamlit run your_script.py
This command will start a local web server and open your app in a new browser tab.
Example: Creating a Simple Calculator
Let's create a simple calculator app using Streamlit. This app will take two numbers as input and display their sum.
import streamlit as st st.title("Simple Calculator") num1 = st.number_input("Enter the first number") num2 = st.number_input("Enter the second number") result = num1 + num2 st.write("The sum is:", result)
In this example, st.number_input
is used to get numeric input from the user, and st.write
is used to display the result.
Conclusion
Creating your first Streamlit app is a straightforward process. By understanding the basics of Streamlit, installing the necessary libraries, and structuring your app correctly, you can quickly build and run your first interactive web application.