writing-streamlit-apps

Write Streamlit app source code that queries PostHog data in a sandboxed runtime.

713|118|Updated Aug 11, 2020
One-click install
npx skills add https://github.com/PostHog/posthog-foss --skill writing-streamlit-apps
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: writing-streamlit-apps
Source: https://github.com/PostHog/posthog-foss/tree/main/products/streamlit_apps/skills/writing-streamlit-apps
Command: npx skills add https://github.com/PostHog/posthog-foss --skill writing-streamlit-apps

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing Streamlit apps for the PostHog sandbox requires knowing the posthog_apps.query() bridge, the fixed package set, and Streamlit's rerun model — mistakes like uncached queries, unsafe string interpolation into HogQL, or importing unavailable packages cause failures that are hard to diagnose from inside the sandbox.

Core Features & Use Cases

  • PostHog data access: Query PostHog with HogQL through the pre-authenticated posthog_apps.query() bridge, which returns pandas DataFrames and raises RuntimeError on failure.
  • Rerun-safe patterns: Cache bridge calls with st.cache_data and persist state with st.session_state so widget interactions don't re-fire every query.
  • Safe parameterization: Constrain widget inputs with selectboxes, sliders, and type coercion instead of interpolating free text into HogQL, preventing viewers from abusing the author's data access.
  • Use Case: Build a single-file app.py that charts daily event counts over a slider-selected day range, with cached queries and st.error handling for failed queries.

Quick Start

Write a Streamlit app that shows daily PostHog event counts for the last 7 days as a bar chart.

Frequently Asked Questions about writing-streamlit-apps

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I query PostHog data from a Streamlit app?

Use posthog_apps.query() with a HogQL string, which returns a pandas DataFrame. The bridge is pre-authenticated to the app's project, so no token or configuration is needed. Wrap calls in st.cache_data to avoid re-running queries on every widget interaction.

How do I cache Streamlit queries across reruns?

Decorate the query function with @st.cache_data(ttl=300) so repeated reruns reuse results instead of re-firing the query. Use st.session_state for values like selections or pagination cursors that must survive reruns, since module-level variables reset.

Can I install extra Python packages in the PostHog Streamlit sandbox?

No, the sandbox never runs pip and drops any uploaded requirements.txt. Only the preinstalled packages are available, including streamlit 1.31, pandas, numpy, polars, plotly, matplotlib, seaborn, scipy, scikit-learn, duckdb, and sqlalchemy.

Why does my Streamlit app query fail with a generic error?

The posthog_apps bridge raises RuntimeError with a deliberately generic message and does not expose query internals to the sandbox. Test HogQL in the SQL editor where real errors are visible, and render failures in the app with st.error(str(e)).

Is it safe to interpolate user input into HogQL queries?

No, never splice free-text widget values into HogQL because viewers of the app would gain the author's data access. Constrain inputs with st.selectbox over fixed values or coerce to safe types like int or date before they reach the query.