icon: LiWrench
Title: Working with Streamlit
import streamlit as st
import pandas as pd
import numpy as np
st.title('Uber pickups in NYC')
DATE_COLUMN = 'date/time'
DATA_URL = ('https://s3-us-west-2.amazonaws.com/'
'streamlit-demo-data/uber-raw-data-sep14.csv.gz')
@st.cache_data
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
data_load_state = st.text('Loading data...')
data = load_data(10000)
data_load_state.text("Done! (using st.cache_data)")
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]
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)
streamlit run hello_world.py
magic command
and how the interactions between the slider and the data filtering work.if you have been following up to this point, you would have most the required understanding to get started on building on your own Streamlit app.
Most of the time, it's about using the right widgets that allow your applications to:
widget
is an interactive element that can be added to your app, allowing users to input data or adjust parameters in real-time. Widgets can range from simple buttons and text inputs to sliders and selectors, and somethings complex like editable tables or 3D rendering.Many of these decisions are specific to your PoC project. As we will be starting to work on the PoC project very soon, it is definitely a good time to check out the different widgets available on Streamlit and scout for the widgets that are potentially useful for your project.
β¦ The best way to find out how the widgets work?
Official API Reference
sectionAs your app grows large, it becomes useful to organize your script into multiple pages. This makes your app easier to manage as a developer and easier to navigate as a user. Streamlit provides a frictionless way to create multipage apps. Pages are automatically shown in a navigation widget inside your app's sidebar. If a user clicks on a page in the sidebar, Streamlit navigates to that page without reloading the frontend β making app browsing incredibly fast! In this guide, letβs learn how to create multipage apps.
Streamlit identifies pages in a multipage app by directory structure and filenames. The file you pass to streamlit run
is called your entrypoint file. This is your app's homepage. When you have a pages/
directory next to your entrypoint file, Streamlit will identify each Python file within it as a page. The following example has three pages. main.py
is the entrypoint file and homepage.
your_working_directory/
βββ pages/
β βββ a_page.py
β βββ another_page.py
βββ main.py
Run your multipage app just like you would for a single-page app.
streamlit run main.py
Only .py
files in the pages/
directory will be identified as pages. Streamlit ignores all other files in the pages/
directory and its subdirectories. Streamlit also ignores Python files in subdirectories of pages/
.
The entrypoint file is your app's homepage and the first page users will see when visiting your app. Once you've added pages to your app, the entrypoint file appears as the topmost page in the sidebar. Streamlit determines the page label and ordering of each page from your filenames.
Filenames are composed of four different parts as follows:
number
. A non-negative integer.separator
. Any combination of underscore ("_"
), dash ("-"
), and space (" "
).label
. Everything up to, but not including, ".py"
.".py"
The entrypoint file
is always displayed first. The remaining pages are sorted as follows:
number
appear before files without a number
.number
(if any), followed by the label
(if any).number
as an actual number rather than a string. So 03
is the same as 3
.This table shows examples of filenames and their corresponding labels, sorted by the order in which they appear in the sidebar.
Filename | Rendered label |
---|---|
1 - first page.py |
first page |
12 monkeys.py |
monkeys |
123.py |
123 |
123_hello_dear_world.py |
hello dear world |
_12 monkeys.py |
12 monkeys |
st.set_page_config
.|In the script for the particular page, we can place this line st.set_page_config(page_title="Your Preferred Label for the Page")
to set the custom label.
Now that you know a little more about all the individual pieces, let's close the loop and review how it works together:
.py
files in a pages
folder.