Skip to content

Instantly share code, notes, and snippets.

View asehmi's full-sized avatar

Arvindra Sehmi asehmi

View GitHub Profile
@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 / 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_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 / 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 / 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 / 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 / printarr
Created February 26, 2023 00:48 — forked from nmwsharp/printarr
Pretty print tables summarizing properties of tensor arrays in numpy, pytorch, jax, etc.
Pretty print tables summarizing properties of tensor arrays in numpy, pytorch, jax, etc.
@asehmi
asehmi / st_button_colour.py
Created March 5, 2023 23:25
Streamlit change button background colour
# Ref: https://discuss.streamlit.io/t/issues-with-background-colour-for-buttons/38723/2?u=asehmi
import streamlit as st
import streamlit.components.v1 as components
def ChangeButtonColour(widget_label, font_color, background_color='transparent'):
htmlstr = f"""
<script>
var elements = window.parent.document.querySelectorAll('button');
for (var i = 0; i < elements.length; ++i) {{
if (elements[i].innerText == '{widget_label}') {{
@asehmi
asehmi / button_open_web_page.py
Created April 13, 2023 11:51
Streamlit URL button
import streamlit as st
from streamlit.components.v1 import html
def open_page(url):
open_script= """
<script type="text/javascript">
window.open('%s', '_blank').focus();
</script>
""" % (url)
html(open_script)
@asehmi
asehmi / embedded_st_app.html
Last active May 18, 2023 17:09
How to cleanly embed a Streamlit app in a web page
<!--
This is a sample HTML file that you can use to embed your Streamlit app in an iframe.
The Streamlit app is embedded cleanly and is almost indistinguishable from a native app.
Use it as a template and customize it to your needs.
NOTE: It's convenient to start your Streamlit app in headless mode, for example
$ streamlit run --server.port=8005 --server.headless=true app.py
-->
<!DOCTYPE html>