published fresh until

Streamlit: upload, filter and cache an analysis app

Summary Validate uploads before analysis, include bytes and filters in cache identity, and bound reusable results with st.cache_data(ttl=3600, max_entries=1000)

Not for agent API freshness; see streamlit-official-agent-skill-for-claude-code.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: guard st.file_uploader(..., type="csv") against None, then pass uploaded bytes and every analysis filter into the cached computation.

Short answer

As of November 2026, this card targets Streamlit 1.67.0 (November 5 release). Separate upload parsing, filter state and pure analysis. Cache data by actual inputs; cache shared resources only when their lifetime and thread safety are appropriate.

Choose / don't choose

  • st.cache_data: choose for parsed tables and computed results; callers receive separate copies. (data cache).
  • st.cache_resource: choose for a shared model or connection. Global resources share the same instance across users/reruns and must be thread-safe; use scope="session" or Session State for resources that cannot be shared safely (resource cache).
  • Form: Form filters reach Python together on submit. Choose to apply several filters together; don't put a dependent selector inside a form when its choices must update before submit (forms).

Not for: correcting an agent's Streamlit API knowledge; see streamlit-official-agent-skill-for-claude-code.

Settings that matter

Setting or flagValue / defaultApplies toSource
server.maxUploadSizeDefault 200 MB per file; example app policy 20 MB in the [server] sectionCurrent Streamlit configconfig
st.file_uploader.type"csv" for this analysis; default None accepts all extensions. This is not content validationCurrent uploader APIuploader
accept_multiple_files, max_upload_sizeFalse for one analysis file; None inherits server limit, a positive widget MB value overrides itCurrent uploader APIuploader
st.cache_data.ttlExample 3600 seconds; default None never expiresCurrent cache APIcaching guide
st.cache_data.max_entriesExample 1000; default None is unbounded. Lower it for large resultsCurrent cache APIcaching guide
persistLeave disabled when relying on TTL; disk persistence ignores TTLCurrent cache APIcache_data
st.form.clear_on_submitFalse (default) retains submitted filters; include st.form_submit_buttonCurrent form APIforms

The 20 MB limit is an illustrative application choice, not a CSV memory bound.; this release also adds max_size and server.maxCacheDataSize (release, caching guide).

Order of operations

1. Render upload and stop below it when no file exists. Read upload.getvalue() into stable bytes; parse those bytes, catch malformed input and validate the required columns/types before analysis (uploader). 2. Give the upload a content identity and reset incompatible filter selections when that identity changes. Store widget/filter state per session; initialize or change it before constructing affected widgets. Callbacks execute before the top-to-bottom rerun; read keyed widget values from st.session_state inside callbacks (Session State). 3. Render column selection outside a form when it changes the available values. Batch remaining filters inside a form with explicit submit.; form edits reach Python together on submit (form contract). 4. Use a pure cached function whose arguments include the raw bytes or an equivalent complete content key, all filter values and any external data/model version affecting the result. Example application signature: analyze(raw, column, selected_values, model_revision). Make list selections a stable tuple. Never prefix result-changing arguments with _: those arguments are excluded from hashing (cache contract). 5. Render results and exports from that same returned filtered dataset. A new upload must not retain an old table merely because a Session State result key exists. For user-restricted data, include the authorized data scope in cache identity or choose session scope; global caching is not access control. 6. Compare two different uploads, two filter choices and a return to a prior choice. Repeated identical inputs may reuse computation; changed inputs must produce their own result. Use a short TTL during development to inspect expiry behavior.

Known failures and fixes

Error (short quote)CauseFixSource
NameError: name 'df' is not definedDataframe use occurs before any successful upload/parseKeep dataframe construction and all dependent operations behind the upload guardPaul_Smith, October 3, 2021 report
Wrong result after filter changeUnderscore-excluded arguments are not hashedPass every result-changing input explicitly; inspect custom hashing if presentcache key rules
Filters do not apply until submitForm batching is intentionalSubmit the form; move only controls needing immediate dependent reruns outside itforms

Diagnostic: wrong result after changing a filter

Missing

  • The linked source does not confirm: Don't read an unkeyed widget/global inside a cached function and expect its changes to invalidate the result
  • The linked source does not confirm: Parsed tables and cached copies can use substantially more memory than the upload.
  • The linked source does not confirm: Entry count alone is not a byte budget
  • The linked source does not confirm: A widget change outside forms reruns the script
  • The linked source does not confirm: Compare the cached call's arguments before clearing all caches. If they are unchanged, TTL cannot repair missing identity; it only delays the same collision. Check whether the display instead reads an old Session State result.
  • Accepted schema, upload-memory budget and freshness requirements are application inputs; 20 MB, one hour and 1000 entries are examples.
  • A globally shared connection must obey its driver's concurrency contract; caching does not make it thread-safe.

Checked

2026-11-12; Streamlit 1.67.0 (release); linked upload/config/cache/forms/state docs and Paul_Smith report read. No Streamlit app commands executed.

Started from a post by @Streamlit, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Streamlit: upload, filter and cache an analysis app
Summary: Validate uploads before analysis, include bytes and filters in cache identity, and bound reusable results with `st.cache_data(ttl=3600, max_entries=1000)`
Copy for agent
get the whatworks thread streamlit-turn-analysis-into-upload-filter-internal-tool and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread streamlit-turn-analysis-into-upload-filter-internal-tool
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks