(go ahead and do it) without hesitatingStreamlit
is very intuitive to use, but the correct configuration of the environment is still essential to realize its full potential.
This post will explain how to configure from scratch theStreamlit
environment, andStreamlit
A few commands commonly used during development.
Finally, a simple example demonstrates the development ofStreamlit
The process of application.
1. Installation
Streamlit
purePython
framework that relies only on thePython
Environment.
Currently the latestStreamlit v1.39
version, which requiresPython3.8
and above.
Streamlit
It has been posted topypiUsepip
Installation is very simple.
pip install streamlit
After the installation is complete, verify that the installation was successful using the following command:
streamlit hello
this oneStreamlit
If the installation is successful, the browser will automatically open after execution.
existhttp://localhost:8501/
Displays the sample project.
There are4 DemosYou can get a feel for it by tapping on it from the left half of the menuStreamlit
The Charm.
2. Common subcommands
Streamlit
There are not many subcommands to pass--help
Parameters can be viewed.
$ streamlit --help
Usage: streamlit [OPTIONS] COMMAND [ARGS]...
Try out a demo with:
$ streamlit hello
Or use the line below to run your own script:
$ streamlit run your_script.py
Options:
--log_level [error|warning|info|debug]
--version Show the version and exit.
--help Show this message and exit.
Commands:
activate Activate Streamlit by entering your email.
cache Manage the Streamlit cache.
config Manage Streamlit's config settings.
docs Show help in browser.
hello Runs the Hello World script.
help Print this help message.
run Run a Python script, piping stderr to Streamlit.
version Print Streamlit's version number.
The most commonly used arerun
subcommand, which is used to execute theStreamlit App
The.run
The subcommand itself has a number of parameters.
For example, the app's IP address, port, theme, logs, auto-reload scripts, and so on.
The following command allows you to view therun
All parameters of the subcommand.
$ streamlit run --help
In addition.config
The subcommand provides a quick view of the current pair ofStreamlit
of all configurations.
$ streamlit config show
cache
subcommand can be used to quickly clear the cache.
$ streamlit cache clear
The above three subcommands are used more often during development.
3. First App
Finally, we use theStreamlit
To do a simple data analysis of the application, in order to experience the power of it.
3.1 Creating test data
First create some test data bypandas
cap (a poem)numpy
establish20 articlesTime series data.
# Create time series test data
A = (1, 80, size=(20, 1))
B = (20, 100, size=(20, 1))
df = ()
= pd.date_range("2024/10/01", periods=20)
df["A"] = A
df["B"] = B
Column Acap (a poem)Column Bis randomly generated data that changes with each run.
3.2 Use of tabular data
Up next.Streamlit
It's time for the debut, the page showspandas
(used form a nominal expression)DataFrame
The data is simple, just one line of code.
# Display data
(df)
Browser Access:http://localhost:8501/
You can add a caption and embellish it a little.
("First App")
() # A dividing line
3.3 Display of data in line graphs
Streamlit
expense or outlaytabularDisplaying the data takes just one line of code, again, withline graphDisplaying the data also takes only one line of code.
# Show line chart
st.line_chart(df)
3.4 Dynamically changing data ranges
Next, add theStreamlit
control that allows us to dynamically change the range of data in tables and line charts.
date_range = (
"Date range",
min_value=datetime(2024, 10, 1),
max_value=datetime(2024, 10, 20),
value=(datetime(2024, 10, 1), datetime(2024, 10, 20)),
)
(date_range)
Add a control with a range of data, and when the range changes, use thedate_range
to update the data to be displayed on the page.
# graph_data is the data filtered by date
graph_data = ()
graph_data = graph_data[graph_data.index >= date_range[0]]
graph_data = graph_data[graph_data.index <= date_range[1]]
The data in the tables and line graphs are changed to the abovegraph_data
。
# Display the line chart
st.line_chart(graph_data)
# Show data
(graph_data)
This allows us to dynamically change data ranges on the page while updating data tables and line graphs.
4. Summary
In just a few lines of code, a display is generatedDataFrame
dataweb application。
together withTraditional Web Development ApproachCompared to not needing any front-end knowledge (HTML
,CSS
cap (a poem)javascript
etc.).
And, by using a wrapped control (table
,line_chart
etc.), development is extremely efficient.
together withJupyter Notebook
Provides a user-friendly interface that is simple and intuitive in comparison.
There is no need for the user to try different charts by modifying the code.
The final complete code for the example is as follows:
import streamlit as st
import pandas as pd
import numpy as np
from datetime import datetime
# Creating time series test data
A = (1, 80, size=(20, 1))
B = (20, 100, size=(20, 1))
df = ()
= pd.date_range("2024/10/01", periods=20)
df["A"] = A
df["B"] = B
("firstAPP")
()
# Add dynamic adjustment of date range
date_range = (
"Date range",
min_value=datetime(2024, 10, 1),
max_value=datetime(2024, 10, 20),
value=(datetime(2024, 10, 1), datetime(2024, 10, 20)),
)
(date_range)
graph_data = ()
graph_data = graph_data[graph_data.index >= date_range[0]]
graph_data = graph_data[graph_data.index <= date_range[1]]
# Show Line Chart
st.line_chart(graph_data)
# Display data
(graph_data)
expense or outlayrun
subcommand to run this script is sufficient.
streamlit run