Streamlit – Self-study (1)

I have briefly learned pyWebio before, but this type of application is only suitable for simple small programs, and there is no way to layout left and right (maybe I didn’t find it), and the application I use now is just the sanger.

Now I want to learn Streamlit, and I hereby record it, because I started from scratch, and most of it is based on official documents. After learning, I will try to write applications by myself.

Install

#Python 3.7 - Python 3.9 #When 
using pip to install, please make sure that the python you often use and the corresponding pip are the same version. I often see that someone has installed it successfully, but when importing, it still reports that the installation was not successful. . 
pip  install streamlit 

#Conda 
config --add channels conda-forge conda config --set channel_priority strict conda install streamlit #If  you use conda to install Build a virtual environment and run tests in it. After all, you have to install 120 packages, which may affect your original packages.

simple application

#!/bin/env python

import streamlit as st
 import pandas as pd
 import numpy as np #The
 installation is complete, no major problems

DATE_COLUMN = 'date/time'
DATA_URL = ('https://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz')

#Cache mechanism, use @st.cache before another function, the user can read it directly when using it for the second time. Of course, the premise is that the parameters passed in are the same. 
@st.cache 
def  load_data (nrows) :
    data = pd.read_csv(DATA_URL, nrows=nrows)
    lowercase = lambda x: str(x).lower()
    data.rename(lowercase, axis='columns', inplace=True)
    data[DATE_COLUMN] = pd.to_datetime(data[DATE_COLUMN])
    return data

# Create a text box to let the user know that data is being loaded 
data_load_state = st.text( 'Loading data...' )
 # Display the first 1000 rows of 
data data = load_data( 1000 )
 # Tell the user that the data has been loaded 
data_load_state.text( 'Done! (using st.cache)' )

#Add a radio box, whether to display the content in the table 
if st.checkbox( 'Show raw data' ):
    st.subheader('Raw data')
    st.write(data)

st.subheader('Number of pickups by hour')
hist_values ​​= np.histogram(data[DATE_COLUMN].dt.hour, bins= 24 , range=( 0 , 24 ))[ 0 ]
 #Draw a histogram
st.bar_chart(hist_values)

# Some number in the range 0-23
hour_to_filter = st.slider('hour', 0, 23, 17)
filtered_data = data[data[DATE_COLUMN].dt.hour == hour_to_filter]

st.subheader('Map of all pickups at %s:00' % hour_to_filter)
st.map(filtered_data)

Leave a Comment

Your email address will not be published. Required fields are marked *