Skip to content

Instantly share code, notes, and snippets.

@ash2shukla
ash2shukla / fastapi_redis_websockets.py
Created May 7, 2023 22:18
Scale websockets with Redis Pubsub in FastAPI
import asyncio
import logging
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.websockets import WebSocket, WebSocketDisconnect
import aioredis
import async_timeout
netsh advfirewall firewall add rule name="Allowing LAN connections" dir=in action=allow protocol=TCP localport=9999
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=9999 connectaddress=localhost connectport=9999
import pandas as pd
def one_hot_column(df, column, labels, prefix=None, **kwargs):
dummies = pd.get_dummies(df[column], prefix=prefix or column, **kwargs)
for label in labels:
if f"{column}_{label}" not in dummies.columns:
dummies[f"{column}_{label}"] = 0
return df.join(dummies)
if __name__ == "__main__":
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.palettes import Category20
from bokeh.plotting import figure
from bokeh.transform import linear_cmap
import numpy as np
import pandas as pd
import streamlit as st
from sklearn.datasets import make_blobs
from state import provide_state
st.set_page_config(layout="wide")
@ash2shukla
ash2shukla / color_change.py
Created October 15, 2020 11:10
Image Color Change
import streamlit as st
import io
from PIL import Image
original, modified = st.beta_columns(2)
image_box = original.empty()
filter_type = original.empty()
image = original.file_uploader("Upload Image")
@ash2shukla
ash2shukla / data.csv
Created October 1, 2020 21:39
data.csv
x y size
0 0.22350554878154982 0.24488387754195973 5.121734927672993
1 0.29228506683932176 0.02701330608229313 7.769701006355811
2 0.1671886828540321 0.4718313957475925 5.433848596895122
3 0.7917289279327249 0.27173221695239047 8.093148016907989
4 0.8375282431440418 0.8498298721856706 9.405192850799946
5 0.5794420093002058 0.28788057347946905 7.977779993621126
6 0.0028331174317425267 0.8954760582866268 1.2970672156251917
7 0.429252587113481 0.554692000881586 3.3975258630268157
8 0.029717719251690977 0.9097710479958356 9.1103289671696
@ash2shukla
ash2shukla / state.py
Last active February 27, 2024 06:00
state decorator
from streamlit.report_thread import get_report_ctx
from streamlit.hashing import _CodeHasher
from streamlit.server.server import Server
from prometheus_client.registry import REGISTRY
from prometheus_client import Counter
class _SessionState:
def __init__(self, session, hash_funcs):
"""Initialize SessionState instance."""
@ash2shukla
ash2shukla / ex.py
Created October 1, 2020 21:35
Streamlit bokeh events example
import streamlit as st
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.plotting import figure
import pandas as pd
import numpy as np
from streamlit_bokeh_events import streamlit_bokeh_events
from state import provide_state
@provide_state
def main(state):