Skip to content

Instantly share code, notes, and snippets.

View asehmi's full-sized avatar

Arvindra Sehmi asehmi

View GitHub Profile
@asehmi
asehmi / form_callback_demo_simple.py
Created December 12, 2022 18:51
Using callbacks and session state in Streamlit forms
import streamlit as st
import pandas as pd
import numpy as np
st.write('`Simple form callback demo`')
st.subheader('Constraints Solver')
state = st.session_state
if 'is_modified' not in state:
state['is_modified'] = False
@asehmi
asehmi / pomodoro2.py
Created November 8, 2022 11:31
A fancy Streamlit Pomodoro count down timer
# Builds on my original Pomodoro timer (see pomodoro.py). This has start, stop, pause controls.
# It uses callbacks, session_state, status messages and reruns for a better UX
# Requires Streamlit, version >= 1.14.0
import streamlit as st
import time
st.set_page_config(
page_title='Pomodoro',
layout='centered',
page_icon='🍅'
@asehmi
asehmi / dataframes_to_xlsx_downloader.py
Created November 5, 2022 12:33
Save dataframes as separate XLSX worksheets in Streamlit
# https://discuss.streamlit.io/t/how-to-add-a-download-excel-csv-function-to-a-button/4474/16
import streamlit as st # 🎈 streamlit development
import pandas as pd
import io
from itertools import cycle
buffer = io.BytesIO()
# Create some Pandas dataframes from some data.
df_list = []
@asehmi
asehmi / st_cascade_selection.py
Last active March 30, 2023 11:20
Streamlit app showing selection boxes with cascading (dependent) options
import json
import streamlit as st
import pandas as pd
st.set_page_config(
page_title='Cascade Selection',
layout='centered',
page_icon='🌴'
)
@asehmi
asehmi / st_javascript_example.py
Last active November 5, 2022 12:29
Streamlit calling JavaScript
# Slightly improved with a couple of js call wrappers to clean code and add async/await boilerplate
import streamlit as st
# https://github.com/thunderbug1/streamlit-javascript
from streamlit_javascript import st_javascript
st.header("st_javascript demo")
st.write("#### Helper wrappers")
with st.echo():
@asehmi
asehmi / st_server_runtime_client_cookies_tests.py
Created October 7, 2022 01:35
Streamlit hack to get current server and client session info (Streamlit >= v1.12.0)
# See discussion: https://github.com/streamlit/streamlit/pull/5457
import re
import streamlit as st
try:
# Streamlit >= 1.12.0
from streamlit.web.server.server import Server
from streamlit.runtime.runtime import Runtime, SessionInfo
from streamlit.runtime.scriptrunner import add_script_run_ctx
# from streamlit.runtime.scriptrunner import get_script_run_ctx
@asehmi
asehmi / mplfinance_demo_advanced.py
Last active February 8, 2023 09:24
Advanced use of mplfinance in Streamlit
import csv
import datetime as dt
import requests
import mplfinance as mpf
import pandas as pd
from pandas_datareader import data as pdr
import streamlit as st
# -----------------------------------------------------------------------------
@asehmi
asehmi / style.py
Created October 7, 2022 01:18
Adjusting Streamlit page container style
import streamlit as st
# --------------------------------------------------------------------------------
def v_space(n, level=None):
for _ in range(n):
header_marks = ''
if level:
header_marks = '#' * int(level)
st.write(f'{header_marks}  '.strip())
@asehmi
asehmi / session_state_callback_demos.py
Last active October 21, 2022 18:31
Streamlit widget session state and callbacks mini-tutorial
#
# This seems to be a common issue amongst new Streamlit users, so I wrote a mini-tutorial app to explain how widgets
# are used with initialized values and how to make them stick using session state and callbacks.
#
# There are three ways:
#
# (1) The most basic where the initial value is not given but the widget is always reset,
# (2) Where it’s initialized but there are issues getting the return value to stick, and finally
# (3) Overcoming all issues with session state and callbacks.
#
@asehmi
asehmi / embedded_flask_server_app.py
Created September 27, 2022 17:24
Embedding Flask in Streamlit
import requests
import streamlit as st
state = st.session_state
if 'flask_started' not in state:
state.flask_started = False
def start_flask():
if state.flask_started:
return