Skip to content

Instantly share code, notes, and snippets.

@skannan-maf
skannan-maf / Dockerfile
Last active June 18, 2025 19:27
How to serve multiple MCP servers through a single app using different URL paths!
# Use the official Python 3.10 image as the base image
FROM python:3.10
# Set the working directory in the container
WORKDIR /app
# Install the "uv" package
RUN curl -Ls https://astral.sh/uv/install.sh | sh
# Initialize the mcp-server using "uv"
@skannan-maf
skannan-maf / Streamlit_scrollToFocusArea.py
Last active February 23, 2025 15:25
Streamlit: Scroll the browser to the screen where the enclosed streamlit code (within the "with" block) executes
from contextlib import contextmanager
import re
@contextmanager
def streamlit_focus(tab_id, snippet_id):
'''
This is a context manager that will focus the widget with the given widget_key
'''
def make_valid_identifier_string(text: str, char_prefix='A') -> str:
"""
@skannan-maf
skannan-maf / stateful_selectbox.py
Last active February 23, 2025 09:42
Streamlit selectbox that works as intended
#
# This gist solves 2 problems
# 1. Streamlit selectbox sometimes does not return correct value correpsonding to what user selected.
# This is a very finicky behavior where the user-selected option, occasionally, reverts back to older option
# and thus violating WYSIWYG. This function fixed it
# 2. In multi-page apps, the user-selected values in selectbox gets lost when user switches to another page.
# This function saves the state and helps these widget maintain state even in multi-page apps
#
def stateful_selectbox(
name,
@skannan-maf
skannan-maf / fuzzy_match_list_of_strings.py
Created February 20, 2025 13:26
Fuzzy match 2 list of strings
import ngram
def fuzzy_match_list_of_strings(
l1,
l2,
common_words=[],
verbose=True
):
'''
Accepts 2 lists of strings as arguments and optionally "common_words" as 3rd argument.
@skannan-maf
skannan-maf / st_select_rows_from_df.py
Created November 28, 2024 05:51
Streamlit select row of a Pandas dataframe
def st_select_rows_from_df(pdf, widget_base_key, exclusive=False, ignore_columns=[]):
'''
This function displays each row with a selectable checkbox
Users can select only one of them (or) any number of them depending on the argument "exclusive"
Function returns a list of booleans indicating whether the corresponding row was selected or not
'''
valid_cols = [col for col in pdf.columns if col not in ignore_columns]
# Write header
view_cols = st.columns(1+len(valid_cols))
import streamlit as st
import pandas as pd
from pandas.api.types import (
is_categorical_dtype,
is_datetime64_any_dtype,
is_numeric_dtype,
is_object_dtype,
)
from datetime import datetime
import time
import streamlit as st
from streamlit.components.v1 import html
#### HEADER: Start a new div
st.markdown('<div id="my-section-{tab_id}"></div>'.format(tab_id = "<your-tab-id>"), unsafe_allow_html=True)
#### PLACEHOLDER: BODY: Render the section as per your app using streamlit primitives (st.plotly_chart
#
# Your Streamlit code rendering whatever you want goes here.
def get_all_cookies():
'''
WARNING: We use unsupported features of Streamlit
However, this is quite fast and works well with
the latest version of Streamlit (1.27)
RETURNS:
Returns the cookies as a dictionary of kv pairs
'''
from streamlit.web.server.websocket_headers import _get_websocket_headers
# https://github.com/streamlit/streamlit/pull/5457
import plotly
import plotly.graph_objects as go
def plotly_display_number(n, what_is_n, color='Black'):
'''
Use this display big numbers in plotly
Automatically recognizes Thousands/million/Billion and displays with a suitable prefix
'''
if n > 1e9:
n /= 1e9
@skannan-maf
skannan-maf / tagged_basket_iterator.py
Created November 17, 2021 11:37
Tagged basket iterator for Doc2vec
# Basket iterator class
class tagged_basket_iterator:
def __init__(self, tb):
self._df = tb._df
self._index = 0
def __next__(self):
total_len = len(self._df)
if self._index >= total_len:
#print("Raising after {}".format(total_len))