Thursday, March 7, 2024

Displaying sidebar and container and graphs with Streamlit

The hierarchy and arrangement of pages on your app can have a large impact on your user experience. 

Passing an element to st.sidebar() will make this element pinned to the left, allowing users to focus on the content in your app.

But st.spinner() and st.echo() are not supported with st.sidebar.

st.container() is used to create an invisible container where you can put elements in order to create a useful arrangement and hierarchy.

st.pyplot(): This function is used to display a matplotlib.pyplot figure.

import streamlit as stimport matplotlib.pyplot as pltimport numpy as nprand=np.random.normal(1, 2, size=20)fig, ax = plt.subplots()ax.hist(rand, bins=15)st.pyplot(fig)

st.line_chart(): This function is used to display a line chart.

import streamlit as stimport pandas as pdimport numpy as npdf= pd.DataFrame(    np.random.randn(10, 2),    columns=['x', 'y'])st.line_chart(df)

st.bar_chart(): This function is used to display a bar chart.

import streamlit as stimport pandas as pdimport numpy as npdf= pd.DataFrame(    np.random.randn(10, 2),    columns=['x', 'y'])st.bar_chart(df)

st.area_chart(): This function is used to display an area chart.

import streamlit as stimport pandas as pdimport numpy as npdf= pd.DataFrame(    np.random.randn(10, 2),    columns=['x', 'y'])st.area_chart(df)

st.altair_chart(): This function is used to display an altair chart.

import streamlit as stimport numpy as npimport pandas as pdimport altair as alt​df = pd.DataFrame(   np.random.randn(500, 3),   columns=['x','y','z'])​c = alt.Chart(df).mark_circle().encode(   x='x' , 'y'=y , size='z', color='z', tooltip=['x', 'y', 'z'])st.altair_chart(c, use_container_width=True)

st.graphviz_chart(): This function is used to display graph objects, which can be completed using different nodes and edges.

import streamlit as stimport graphviz as graphvizst.graphviz_chart('''    digraph {        Big_shark -> Tuna        Tuna -> Mackerel        Mackerel -> Small_fishes        Small_fishes -> Shrimp    }''')

refenrences:

https://www.datacamp.com/tutorial/streamlit

No comments:

Post a Comment