Location>code7788 >text

『Play Streamlit』--Environmental Configuration

Popularity:197 ℃/2024-10-17 11:36:37

(go ahead and do it) without hesitatingStreamlitis 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 theStreamlitenvironment, andStreamlitA few commands commonly used during development.

Finally, a simple example demonstrates the development ofStreamlitThe process of application.

1. Installation

StreamlitpurePythonframework that relies only on thePythonEnvironment.

Currently the latestStreamlit v1.39version, which requiresPython3.8and above.

StreamlitIt has been posted topypiUsepipInstallation is very simple.

pip install streamlit 

After the installation is complete, verify that the installation was successful using the following command:

streamlit hello

this oneStreamlitIf 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 menuStreamlitThe Charm.

2. Common subcommands

StreamlitThere are not many subcommands to pass--helpParameters 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 arerunsubcommand, which is used to execute theStreamlit AppThe.runThe 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 therunAll parameters of the subcommand.

$  streamlit run --help

In addition.configThe subcommand provides a quick view of the current pair ofStreamlitof all configurations.

$  streamlit config show

cachesubcommand 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 theStreamlitTo 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 bypandascap (a poem)numpyestablish20 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.StreamlitIt's time for the debut, the page showspandas(used form a nominal expression)DataFrameThe 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

Streamlitexpense 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 theStreamlitcontrol 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_rangeto 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 generatedDataFramedataweb application

together withTraditional Web Development ApproachCompared to not needing any front-end knowledge (HTMLCSScap (a poem)javascriptetc.).

And, by using a wrapped control (tableline_chartetc.), development is extremely efficient.

together withJupyter NotebookProvides 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 outlayrunsubcommand to run this script is sufficient.

streamlit run