Full Blog TOC

Full Blog Table Of Content with Keywords Available HERE

Tuesday, November 5, 2024

Streamlit


 


In this post we present a simple example of a streamlit based application. Streamlit is a framework providing simple and fast GUI development for internal use. The nice thing is that it is very simple, the coding is pure python without any Javascript and without multiple processes, Notice that the GUI is not for the end user, but mostly for internal developers use, since due to its simplicity it is also limited.



The following code handles configuration for a long running process, and rerunning the process if and only if the configuration changes. Some screenshots are below:








The simple python code is below.


import random
import time
from datetime import datetime

import pandas as pd
import streamlit as st


def display_bars(amount):
data = []
for i in range(amount):
data.append(['person {}'.format(i), random.randint(10, 50)])
df = pd.DataFrame(data, columns=['Name', 'Age'])
st.bar_chart(df, x="Name", y="Age")


@st.cache_data
def run_long_processing(compute_time):
progress = st.empty()

with progress.container():
st.text('starting with {} times'.format(compute_time))
for i in range(compute_time):
time.sleep(1)
st.text('iteration {}'.format(i))
time.sleep(1)
progress.empty()

st.text('task with {} times is complete'.format(compute_time))
display_bars(compute_time)


@st.fragment(run_every='1s')
def display_time():
st.write(datetime.now())


def main():
st.title('Demo Application')
display_time()

if st.button('clear cache'):
st.cache_data.clear()

tab1, tab2 = st.tabs(['config', 'results'])

with tab1:
st.session_state['compute_time'] = st.slider('select compute time', 1, 10, 3)
with tab2:
compute_time = st.session_state['compute_time']
run_long_processing(compute_time)


main()