Skip to content

Latest commit

 

History

History
3535 lines (3463 loc) · 118 KB

streamlite_api.md

File metadata and controls

3535 lines (3463 loc) · 118 KB

API reference

Streamlit makes it easy for you to visualize, mutate, and share data. The API reference is organized by activity type, like displaying data or optimizing performance. Each section includes methods associated with the activity type, including examples.

Browse our API below and click to learn more about any of our available commands! 🎈

Display almost anything

st.write

Write arguments to the app.


st
.
write
(

"Hello **world**!"

)

st . write ( my_data_frame )

st . write ( my_mpl_figure )

Copy

Magic

Any time Streamlit sees either a variable or literal value on its own line, it automatically writes that to your app using st.write



"Hello **world**!"

my_data_frame my_mpl_figure

Copy

Text elements

screenshot

Markdown

Display string formatted as Markdown.


st
.
markdown
(

"Hello **world**!"

)

Copy
screenshot

Title

Display text in title formatting.


st
.
title
(

"The app title"

)

Copy
screenshot

Header

Display text in header formatting.


st
.
header
(

"This is a header"

)

Copy
screenshot

Subheader

Display text in subheader formatting.


st
.
subheader
(

"This is a subheader"

)

Copy
screenshot

Caption

Display text in small font.


st
.
caption
(

"This is written small caption text"

)

Copy
screenshot

Code block

Display a code block with optional syntax highlighting.


st
.
code
(

"a = 1234"

)

Copy
screenshot

Preformatted text

Write fixed-width and preformatted text.


st
.
text
(

"Hello world"

)

Copy
screenshot

LaTeX

Display mathematical expressions formatted as LaTeX.


st
.
latex
(

"\int a x^2 \,dx"

)

Copy

Data display elements

screenshot

Dataframes

Display a dataframe as an interactive table.


st
.
dataframe
(
my_data_frame
)

Copy
screenshot

Static tables

Display a static table.


st
.
table
(
my_data_frame
)

Copy
screenshot

Metrics

Display a metric in big bold font, with an optional indicator of how the metric changed.


st
.
metric
(

"My metric"

,
 
42

,
 
2

)

Copy
screenshot

Dicts and JSON

Display object or string as a pretty-printed JSON string.


st
.
json
(
my_data_frame
)

Copy

Chart elements

screenshot

Simple line charts

Display a line chart.


st
.
line_chart
(
my_data_frame
)

Copy
screenshot

Simple area charts

Display an area chart.


st
.
area_chart
(
my_data_frame
)

Copy
screenshot

Simple bar charts

Display a bar chart.


st
.
bar_chart
(
my_data_frame
)

Copy
screenshot

Scatterplots on maps

Display a map with points on it.


st
.

map

(
my_data_frame
)

Copy
screenshot

Matplotlib

Display a matplotlib.pyplot figure.


st
.
pyplot
(
my_mpl_figure
)

Copy
screenshot

Altair

Display a chart using the Altair library.


st
.
altair_chart
(
my_altair_chart
)

Copy
screenshot

Vega-Lite

Display a chart using the Vega-Lite library.


st
.
vega_lite_chart
(
my_vega_lite_chart
)

Copy
screenshot

Plotly

Display an interactive Plotly chart.


st
.
plotly_chart
(
my_plotly_chart
)

Copy
screenshot

Bokeh

Display an interactive Bokeh chart.


st
.
bokeh_chart
(
my_bokeh_chart
)

Copy
screenshot

PyDeck

Display a chart using the PyDeck library.


st
.
pydeck_chart
(
my_pydeck_chart
)

Copy
screenshot

GraphViz

Display a graph using the dagre-d3 library.


st
.
graphviz_chart
(
my_graphviz_spec
)

Copy

Input widgets

screenshot

Button

Display a button widget.


clicked 
=
 st
.
button
(

"Click me"

)

Copy
screenshot

Download button

Display a download button widget.


st
.
download_button
(

"Download file"

,
 
file

)

Copy
screenshot

Checkbox

Display a checkbox widget.


selected 
=
 st
.
checkbox
(

"I agree"

)

Copy
screenshot

Radio

Display a radio button widget.


choice 
=
 st
.
radio
(

"Pick one"

,
 
[

"cats"

,
 
"dogs"

]

)

Copy
screenshot

Selectbox

Display a select widget.


choice 
=
 st
.
selectbox
(

"Pick one"

,
 
[

"cats"

,
 
"dogs"

]

)

Copy
screenshot

Multiselect

Display a multiselect widget. The multiselect widget starts as empty.


choices 
=
 st
.
multiselect
(

"Buy"

,
 
[

"milk"

,
 
"apples"

,
 
"potatoes"

]

)

Copy
screenshot

Slider

Display a slider widget.


number 
=
 st
.
slider
(

"Pick a number"

,
 
0

,
 
100

)

Copy
screenshot

Select-slider

Display a slider widget to select items from a list.


size 
=
 st
.
select_slider
(

"Pick a size"

,
 
[

"S"

,
 
"M"

,
 
"L"

]

)

Copy
screenshot

Text input

Display a single-line text input widget.


name 
=
 st
.
text_input
(

"First name"

)

Copy
screenshot

Number input

Display a numeric input widget.


choice 
=
 st
.
number_input
(

"Pick a number"

,
 
0

,
 
10

)

Copy
screenshot

Text-area

Display a multi-line text input widget.


text 
=
 st
.
text_area
(

"Text to translate"

)

Copy
screenshot

Date input

Display a date input widget.


date 
=
 st
.
date_input
(

"Your birthday"

)

Copy
screenshot

Time input

Display a time input widget.


time 
=
 st
.
time_input
(

"Meeting time"

)

Copy
screenshot

File Uploader

Display a file uploader widget.


data 
=
 st
.
file_uploader
(

"Upload a CSV"

)

Copy
screenshot

Camera input

Display a widget that allows users to upload images directly from a camera.


image 
=
 st
.
camera_input
(

"Take a picture"

)

Copy
screenshot

Color picker

Display a color picker widget.


color 
=
 st
.
color_picker
(

"Pick a color"

)

Copy

Media elements

screenshot

Image

Display an image or list of images.


st
.
image
(
numpy_array
)

st . image ( image_bytes )

st . image ( file )

st . image ( "https://example.com/myimage.jpg" )

Copy
screenshot

Audio

Display an audio player.


st
.
audio
(
numpy_array
)

st . audio ( audio_bytes )

st . audio ( file )

st . audio ( "https://example.com/myaudio.mp3" , format = "audio/mp3" )

Copy
screenshot

Video

Display a video player.


st
.
video
(
numpy_array
)

st . video ( video_bytes )

st . video ( file )

st . video ( "https://example.com/myvideo.mp4" , format = "video/mp4" )

Copy

Layouts and containers

screenshot

Sidebar

Display items in a sidebar.


st
.
sidebar
.
write
(

"This lives in the sidebar"

)

st . sidebar . button ( "Click me!" )

Copy
screenshot

Columns

Insert containers laid out as side-by-side columns.


col1
,
 col2 
=
 st
.
columns
(

2

)

col1 . write ( "this is column 1" )

col2 . write ( "this is column 2" )

Copy
screenshot

Expander

Insert a multi-element container that can be expanded/collapsed.



with
 st
.
expander
(

"Open to see more"

)

:

st . write ( "This is more content" )

Copy
screenshot

Container

Insert a multi-element container.


c 
=
 st
.
container
(

)

st . write ( "This will show last" )

c . write ( "This will show first" )

c . write ( "This will show second" )

Copy
screenshot

Empty

Insert a single-element container.


c 
=
 st
.
empty
(

)

st . write ( "This will show last" )

c . write ( "This will be replaced" )

c . write ( "This will show first" )

Copy

Display progress and status

screenshot

Progress bar

Display a progress bar.



for
 i 
in
 
range

(

101

)

:

st . progress ( i )

do_something_slow ( )

Copy
screenshot

Spinner

Temporarily displays a message while executing a block of code.



with
 st
.
spinner
(

"Please wait..."

)

:

do_something_slow ( )

Copy
screenshot

Balloons

Display celebratory balloons!


do_something
(

)

# Celebrate when all done!

st . balloons ( )

Copy
screenshot

Snowflakes

Display celebratory snowflakes!


do_something
(

)

# Celebrate when all done!

st . snow ( )

Copy
screenshot

Error box

Display error message.


st
.
error
(

"We encountered an error"

)

Copy
screenshot

Warning box

Display warning message.


st
.
warning
(

"Unable to fetch image. Skipping..."

)

Copy
screenshot

Info box

Display an informational message.


st
.
info
(

"Dataset is updated every day at midnight."

)

Copy
screenshot

Success box

Display a success message.


st
.
success
(

"Match found!"

)

Copy
screenshot

Exception output

Display an exception.


e 
=
 RuntimeError
(

"This is an exception of type RuntimeError"

)

st . exception ( e )

Copy

Control flow

Forms

Create a form that batches elements together with a “Submit” button.



with
 st
.
form
(
key
=

'my_form'

)

:
username <span class="token operator">

= st . text_input ( "Username" )

password <span class="token operator">

= st . text_input ( "Password" )

st<span class="token punctuation">

. form_submit_button ( "Login" )

Copy

Stop execution

Stops execution immediately.


st
.
stop
(

)

Copy

Rerun script

Rerun the script immediately.


st
.
experimental_rerun
(

)

Copy

Utilities

Set page title, favicon, and more

Configures the default settings of the page.


st
.
set_page_config
(

title = "My app" ,

favicon = ":shark:" ,

)
Copy

Echo

Display some code on the app, then execute it. Useful for tutorials.



with
 st
.
echo
(

)

:

st . write ( 'This code will be printed' )

Copy

Get help

Display object’s doc string, nicely formatted.


st
.

help

(
st
.
write
)

st . help ( pd . DataFrame )

Copy

st.experimental_show

Write arguments and argument names to your app for debugging purposes.


df 
=
 pd
.
DataFrame
(

{
  
'first column'

:
 
[

1

,
 
2

,
 
3

,
 
4

]

,
  
'second column'

:
 
[

10

,
 
20

,
 
30

,
 
40

]

,
 
}

)

st . experimental_show ( df )

Copy

Get query paramters

Return the query parameters that are currently showing in the browser's URL bar.


st
.
experimental_get_query_params
(

)

Copy

Set query paramters

Set the query parameters that are shown in the browser's URL bar.


st
.
experimental_set_query_params
(

show_map = True ,

selected = [ "asia" ]

)
Copy

Mutate charts

Add rows

Append a dataframe to the bottom of the current one in certain elements, for optimized data updates.


element 
=
 st
.
line_chart
(
df
)

element . add_rows ( df_with_extra_rows )

Copy

State management

Session state

Session state is a way to share variables between reruns, for each user session.


st
.
session_state
[

'key'

]
 
=
 value
Copy

Performance

Caching

Function decorator to memoize function executions.



@st
.
cache

(
ttl
=

3600

)

def
 
run_long_computation

(
arg1
,
 arg2
)

:
  
# Do stuff here
  
return
 computation_output
Copy

Memo

Experimental function decorator to memoize function executions.



@st
.
experimental_memo

def
 
fetch_and_clean_data

(
url
)

:
  
# Fetch data from URL here, and then clean it up.
  
return
 data
Copy

Singleton

Experimental function decorator to store singleton objects.



@st
.
experimental_singleton

def
 
get_database_session

(
url
)

:
  
# Create a database session object that points to the URL.
  
return
 session
Copy

Clear memo

Clear all in-memory and on-disk memo caches.



@st
.
experimental_memo

def
 
fetch_and_clean_data

(
url
)

:
  
# Fetch data from URL here, and then clean it up.
  
return
 data

if
 st
.
checkbox
(

"Clear All"

)

:
  
# Clear values from *all* memoized functions

st . experimental_memo . clear ( )

Copy

Clear singleton

Clear all singleton caches.



@st
.
experimental_singleton

def
 
get_database_session

(
url
)

:
  
# Create a database session object that points to the URL.
  
return
 session

if
 st
.
button
(

"Clear All"

)

:
  
# Clears all singleton caches:

st . experimental_singleton . clear ( )

Copy

Personalization

User info

st.experimental_user returns information about the logged-in user of private apps on Streamlit Cloud.



if
 st
.
experimental_user
.
email 
==
 
"foo@corp.com"

:

st . write ( "Welcome back, " , st . experimental_user . email )

else :

st . write ( "You are not authorized to view this page." )

Copy

Was this page helpful?

thumb_up Yes thumb_down No
edit Suggest edits