Creating and publishing a Streamlit application that renders data based on widget values

pythonsnowflake

Streamlit is a tool for developing data applications in Python.

$ pip install streamlit
$ streamlit --version
Streamlit, version 1.41.1

You can display Dataframes using write() and line_chart() based on input values from widgets such as buttons, sliders, and radio buttons. Since updates are triggered every time widget values change, you can store values in session_state or cache the results of heavy processing as needed.

import streamlit as st
import pandas as pd

st.write('''
## Hello!
[sambaiz.net](https://www.sambaiz.net/)
''')

first_column, second_column, third_column = st.columns(3, gap='large')

if 'mutiplier' not in st.session_state:
    st.session_state['mutiplier'] = 1

with first_column:
    if st.button('Multiply!'):
        st.session_state['mutiplier'] *= 2
    st.success(f'Multiplier updated to {st.session_state["mutiplier"]}')

with second_column:
    value = second_column.slider('Set value', 0, 100 * st.session_state['mutiplier'])
    st.metric('slider value', value)

with third_column:
    chosen = st.radio('Chose value', ('X', 'Y', 'Z'))
    st.metric('chosen value', chosen)

df = pd.DataFrame({
    'first column': list(filter(lambda x: x != chosen, ['W', 'X', 'Y', 'Z'])),
    'second column': [1 / max(value, 1e-9), value, 10 * value]
})

st.write(df)
st.line_chart(df.set_index('first column'))

Execute it by running streamlit run app.py.

While you can deploy to any environment like EC2, for personal applications you can publish for free on Community Cloud. It’s easy since you just need to push to GitHub and specify the filename.

Additionally, it’s integrated with Snowflake where you can run it by uploading your script.

import streamlit as st
from snowflake.snowpark.context import get_active_session

session = get_active_session()

df = session.sql("""SELECT 1 AS one""").collect()
st.write(df)