Lending Club loans, in plain English
A querychat sidebar (Claude, via chatlas) sits next to a plain shinywidgets dashboard of 10,000 Lending Club loans: a compact stat strip and three render_plotly cards, all driven by qc.df(), querychat’s current filtered frame. The shinywidgets-specific twist is the full-width hero scatter (interest rate vs. loan amount, colored by grade – Lending Club prices loans almost entirely off their sub-grade, so each grade renders as its own clean horizontal band): lasso a band and an “Ask about these N loans” pill submits a question to the chat, which calls a registered describe_selected_loans tool that reads the scatter’s live selection – the same accessor the dashboard itself would read – so the answer always matches what’s actually lassoed, under whatever filter is currently applied.


Run it
git clone --depth 1 https://github.com/posit-dev/py-shinywidgets
cd py-shinywidgets/website/gallery/showcases/loan-chat
uv run --with querychat --with chatlas --with anthropic --with python-dotenv shiny run app.pyChat needs an Anthropic API key: copy .env.example to .env and add your key there (the app loads it with python-dotenv). Without a key, the dashboard still renders and the sidebar explains how to enable chat. data/loans.parquet is committed, trimmed from OpenIntro’s CSV by prep_data.py (state, income, grade, purpose, and 12 other columns, plus a synthetic loan_id).
Try this
- Click a Filter suggestion (“Show California loans graded D or worse”) and watch the stat strip and all three lower charts narrow to the matching 237 loans.
- Click an Ask suggestion (“Which loan purpose has the highest average interest rate?”) for a plain-English answer computed from the live data, not a canned fact.
- Lasso a horizontal band on the hero scatter (grade C sits around 12-16%) and click the Ask about these N loans pill that appears in the card header. The chat calls
describe_selected_loans()and summarizes exactly what you selected – medians, grade mix, top purposes – in at most a sentence and three bullets, no hedging. - A filter that matches nothing (e.g. “Show Vermont loans graded F or G”) zeroes the stat strip and swaps every chart for a “No loans match this filter” message instead of an empty plot.
Accessors demonstrated
scatter_chart.selected_rows(df, key="loan_id")– matches the lasso’scustomdata(set viacustom_data=["loan_id", "loan_amount"]; the key is always the first entry) back to rows of whatever frame you pass, so it works correctly after querychat re-filters and the scatter re-renders with new data. A re-render reseeds the selection to empty (see the widget’s own docs), which is exactly the behavior that keepsdescribe_selected_loans()honest: the tool always reads the selection on the current scatter, never a stale one from before a filter changed what’s plotted.qc.server(client=...).client.register_tool(...)– registering an extra tool directly on querychat’s own per-session chat client, alongside its built-infilterandquerytools, so one conversation can both explore the data and reason about a widget gesture.shinychat.Chat.update_user_input(value=..., submit=True)– the “Ask about these N loans” pill composes a question from the live selection count and submits it to the chat programmatically, the same as if the user had typed and pressed enter.