Skip to content

download_button with data=callable runs without ScriptRunContext #13409

@DavidWalz

Description

@DavidWalz

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

When using the newly introduced option to provide a callable as data argument, the callable runs without ScriptRunContext.

Reproducible Code Example

# app.py
import csv
import io

import streamlit as st


def to_csv() -> str:
    foo = st.session_state.get("foo", "nope")
    print(foo)
    buffer = io.StringIO()
    writer = csv.writer(buffer)
    writer.writerow([foo])
    buffer.seek(0)
    return buffer.getvalue()


st.download_button(
    label="Download",
    data=to_csv,
    file_name="foo.csv",
    mime="text/csv",
)

st.session_state["foo"] = "bar"

Steps To Reproduce

  1. streamlit run app.py
  2. Click the button

Raises Thread 'asyncio_0': missing ScriptRunContext! This warning can be ignored when running in bare mode.

Expected Behavior

When called from the download_button, the function to_csv should have context, and see the currenct session_state.

Current Behavior

Warning

Thread 'asyncio_0': missing ScriptRunContext! This warning can be ignored when running in bare mode.

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.52.2
  • Python version: 3.13
  • Operating System: Win
  • Browser: Chrome

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triageIssue has not been triaged by the Streamlit teamtype:bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions