Building a Bashboard Web App in Python

Resources

Introduction

Streamlit

poetry new web-app # create new poetry python virtual
poetry add streamlit # install streamlit
poetry run streamlit hello # test run streamlit

st.write() is similar to a print() function and has the ability to print several Python objects to your web app.

Interactive plotting libraries such as Altair performs much faster than Plotly and Matplotlib. Streamlit can send json from the server to the client, and the client is going to render the json.

Optimization

The script is going to re-run from top to bottom each time there is a user interaction. This can be problematic if you load a dataset and don’t want to re-load it. Here we can use st.session_state. E.g., if we want to do nested buttons, then when the user clicks the second button, the first button goes back to it’s original declaration.

The @st.cache, st.experimental_memo, and st.experimental_singleton decorators can be used such that an operation is not re-run if the input did not change.

CSS styling in Streamlit

Every Streamlit widget maps to html/CSS code in the browser. With Web inspect it’s possible to figure out and see what maps to what. This makes it possible to over-ride the initial styling. st.markdown is able to process html code and applies CSS code globally.

st.markdown(
    """
    <style>
    .stApp {
        background-color: green;
    }
    </style>
    """
    unsafe_allow_html=True

The problem with using CSS styling in Streamlit, is that it’s dependent on the Steamlit devs not changing the names and structure of the widgets. If they change a widget’s name to something else, your styling will break, and you’ll need to inspect the new objects and change your code.

Frameworks overview

Bokeh

Voila

Plotly’s Dash

Streamlit

Flask

Shiny