Skip to content

Instantly share code, notes, and snippets.

View asehmi's full-sized avatar

Arvindra Sehmi asehmi

View GitHub Profile
@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
@asehmi
asehmi / image-grid.py
Created July 10, 2022 15:58
Multi-column image grid with Streamlit
from itertools import cycle
import streamlit as st
num_cols = int(st.sidebar.number_input('Number columns', 1, 7, 3, 1))
img_w = int(st.sidebar.number_input('Image width', 50, 500, 150, 10))
images = {
'https://unsplash.com/photos/-YHSwy6uqvk/download?force=true&w=640': 'Feast',
'https://unsplash.com/photos/UC0HZdUitWY/download?force=true&w=640': 'Mixed Grill',
@asehmi
asehmi / multi-column-widgets-with-continuous-update.py
Created May 24, 2022 14:06
Using Streamlit widgets in a multi-column table format (dynamic update)
# My solution for: https://discuss.streamlit.io/t/how-to-update-st-columns/25501/3
# This uses a button callback to report the enroll action. Notice how all the widget keys are
# generated so they are unique. To deal with being able to edit the values from their defaults,
# and report on them correctly, I am printing out the widgets’ session state values.
# UPDATE: Added continuous update of the students list
import streamlit as st
from dataclasses import dataclass
import time
import random
@asehmi
asehmi / multi-column-widgets.py
Created May 24, 2022 11:01
Using Streamlit widgets in a multi-column table format
# My solution for: https://discuss.streamlit.io/t/how-to-update-st-columns/25501/3
# This uses a button callback to report the enroll action. Notice how all the widget keys are
# generated so they are unique. To deal with being able to edit the values from their defaults,
# and report on them correctly, I am printing out the widgets’ session state values.
import streamlit as st
from dataclasses import dataclass
state = st.session_state
@asehmi
asehmi / widget_callbacks_sliders.py
Created May 20, 2022 15:00
Setting Streamlit widget values via callbacks (2 range sliders | no value overlap | clamped values)
# 2 range sliders | no value overlap | clamped values
import streamlit as st
state = st.session_state
def _set_x_cb():
if state.x[1] > state.y[0]:
state.x = (state.x[0], state.y[0])
def _set_y_cb():
if state.y[0] < state.x[1]:
@asehmi
asehmi / widget_callbacks_date_input.py
Created May 19, 2022 14:06
Setting Streamlit widget values via callbacks (dates examples)
# Based on discussion here: https://discuss.streamlit.io/t/how-to-work-date-input-with-shortcuts/25377/3
import streamlit as st
from datetime import datetime as dt
import calendar
state = st.session_state
if 'message' not in state:
state.message = ''
@asehmi
asehmi / check_streamlit_context.py
Created May 19, 2022 13:29
Check if an app is running under Streamlit or native Python
def can_use_streamlit():
"""
Function to check whether python code is run within streamlit
Requires Streamlit>=1.9.0
Returns
-------
can_use_streamlit : boolean
True if code is run within streamlit, else False
@asehmi
asehmi / widget_callbacks.py
Last active May 19, 2022 12:44
Setting Streamlit widget values via callbacks
# Generic answer to this question: https://discuss.streamlit.io/t/how-to-work-date-input-with-shortcuts/25377
import streamlit as st
state = st.session_state
def _set_start_cb():
if state.start > state.end:
state.start = state.end
def _set_end_cb():
if state.end < state.start:
@asehmi
asehmi / spacy-word-match-viz.py
Last active May 19, 2022 14:15
Streamlit app using spaCy's rule-based matcher to find weak phrases in requirements statements
# My solution to this: https://discuss.streamlit.io/t/the-if-else-appear-anyway/24713/7
import streamlit as st
import spacy
from spacy import displacy
from spacy.matcher import PhraseMatcher
nlp = spacy.load('en_core_web_sm') #some alternatives en_core_web_md, en_core_web_lg
nlp.disable_pipes('ner')
@asehmi
asehmi / amwtmu_demo.md
Last active May 11, 2022 16:45
scraPy spaCy demo