Skip to content

Instantly share code, notes, and snippets.

@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))
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
'''
Getting selectbox working in streamlit can be tricky.
1)
Sometimes, when we select an option, streamlit suddenly changes (in a quick flash) to the old option.
This can be unnerving at times.
This is because when streamlit re-renders your script after the option selection, the "widget key" in session state
is already populated with the value of new option.
So you need to pass that option's index while creating the selectbox.
2)
In a multi-page streamlit app, streamlit forgets the selected option when the user navigates away from a page
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 time
import streamlit as st
from streamlit.components.v1 import html
st.markdown('<div id="my-section-{tab_id}"></div>'.format(tab_id = "<your-tab-id>"), unsafe_allow_html=True)
#### Render the section as per your app
#### At the end, include JS that shifts attention to the section you created above
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