Skip to content

Instantly share code, notes, and snippets.

@cpsievert
cpsievert / README.md
Last active June 15, 2026 17:19
QueryChat+Snowflake Loan Data demo

Shiny for Python Loan Analysis Dashboard - Snowflake

A Python Shiny web application for analyzing loan data with interactive visualizations and state-level statistics.

This dashboard features an LLM chat sidebar powered by querychat that allows the viewer to ask questions to dynamically generate SQL sent to Snowflake using ibis and SQLAlchemy to find results.

Overview

This dashboard provides an interactive interface to explore and analyze Lending Club loan data. It features:

@cpsievert
cpsievert / plan.md
Created June 2, 2026 16:03
Gray Screen of Death

Session Persistence: Eliminating the Gray Screen of Death

Problem

When a Shiny app's WebSocket connection drops — whether from a network blip, laptop sleep, server error, or any other cause — the user sees a semi-transparent gray overlay with no recovery path (the "gray screen of death"). The session is permanently destroyed server-side, and all state is lost.

An existing session$allowReconnect() mechanism exists but is off by default, only works on certain hosting platforms, and performs a cold restart (new session, server function re-runs, all server-side state lost).

Goal

@cpsievert
cpsievert / app.py
Created February 18, 2025 23:11
A real example of py-shiny's MarkdownStream() + chatlas
from chatlas import ChatAnthropic
from faicons import icon_svg
from shiny import reactive
from shiny.express import input, render, ui
chat_model = ChatAnthropic(
system_prompt="""
You are a helpful AI fitness coach.
Give detailed workout plans to users based on their fitness goals and experience level.
Before getting into details, give a brief introduction to the workout plan.
@cpsievert
cpsievert / rag-chatlas.py
Last active December 13, 2024 20:42
Simple RAG with chatlas
import numpy as np
from chatlas import ChatAnthropic
from sentence_transformers import SentenceTransformer
# Initialize the SentenceTransformer model and chat client
embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
chat = ChatAnthropic(
system_prompt="""
You are a helpful AI assistant. Using the provided context,
answer the user's question. If you cannot answer the question based on the
@cpsievert
cpsievert / README.md
Last active September 17, 2024 22:18
Minimal reprex for shinywidgets on shinyapps.io issue

This gist contains a minimal reprex for this CS ticket

The app.py was deployed here. If you visit there, and repeatedly refresh, you should (eventually) see this error:

Screenshot 2024-09-17 at 3 20 15 PM

Note that the reprex needs this branch of shinywidgets, which switches the order of the libembed-amd.js and output.js scripts in the head of the document.

For some reason, quarto serve seems to be ordering these `

@cpsievert
cpsievert / app.py
Created July 15, 2024 15:07
Filter DataGrid using plotly scatterplot
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from shinywidgets import render_plotly
from shiny import reactive
from shiny.express import render
num_rows = 10
x_points = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@cpsievert
cpsievert / app.py
Created May 21, 2024 15:14
Access click on plotly choroplethmapbox
# Example adapted from https://plotly.com/python/mapbox-county-choropleth/
import json
from urllib.request import urlopen
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from shinywidgets import render_plotly
@cpsievert
cpsievert / 01-tooltips-popovers.R
Created August 10, 2023 15:55
Code examples for bslib 0.5.1 announcement blog post
# ------------------------------------------------------------------------
# Learn more about bslib::tooltip() and bslib::popover() at
# https://rstudio.github.io/bslib/articles/tooltips-popovers/index.html
# ------------------------------------------------------------------------
library(shiny)
library(bslib)
library(palmerpenguins)
library(ggplot2)
library(bslib)
library(htmltools)
library(crosstalk)
library(plotly)
library(leaflet)
# Create Diamonds view
dat <- SharedData$new(dplyr::sample_n(diamonds, 1000))
sidebar_diamonds <- layout_sidebar(
library(bslib)
library(bsicons)
library(crosstalk)
library(plotly)
dat <- SharedData$new(dplyr::sample_n(diamonds, 1000))
accordion_filters <- accordion(
accordion_panel(
"Dropdowns", icon = bs_icon("menu-app"),