Skip to main content

st-copy-button

A simple Streamlit component for copying text to the user's clipboard with one click.

It renders a small button styled to match your app's active Streamlit theme, in light and dark mode alike, and reports back whether the copy succeeded.

Installation

pip install st-copy-button

Quick start

import streamlit as st
from st_copy_button import st_copy_button

st_copy_button("Copy this to clipboard")

Use as many as you like — each button gets its own identity automatically:

for name, token in tokens.items():
    st.write(name)
    st_copy_button(token)

Customise the labels, and optionally show the text itself as a second, also-clickable button to the left of the copy button:

st_copy_button(
    text="Custom text",
    before_copy_label="📋 Push to copy",
    after_copy_label="✅ Text copied!",
    show_text=True,
)

The return value

st_copy_button() returns three different things, so check for True explicitly rather than treating the result as a plain boolean:

Value Meaning
None The button was not clicked on this run.
True It was clicked and the text reached the clipboard.
False It was clicked but the copy failed — most often because the browser withheld clipboard access (see Requirements).
if st_copy_button("some text") is True:
    st.toast("Copied!")

Two things to know about the timing:

  • A click reruns your app. The return value is available on that rerun, not in the same run as the click.
  • The value lasts exactly one run. The run after a click reports None again, so a click won't re-fire on every subsequent rerun.

API

st_copy_button(text, before_copy_label="📋", after_copy_label="✅", show_text=False, disabled=False, key=None, help=None, *, on_error_label="❌", type="secondary", width="content", aria_label=None)
Parameter Type Default Description
text str required The text to copy. Leading and trailing whitespace is stripped before it reaches the clipboard. Anything other than a str is copied as its str() form, except None, which is not copied and fails the same way as a denied copy (see Requirements).
before_copy_label str "📋" The label shown before a copy, and again about a second afterwards.
after_copy_label str "✅" The label shown briefly after a successful copy.
show_text bool False Also render text as a second, clickable button to the left of the copy button.
disabled bool False Disables the button so it cannot be clicked.
key str or None None Identifies this button among all elements in the app. See below.
help str or None None A tooltip shown when the button is hovered, as in st.button. It is the browser's own plain-text tooltip rather than Streamlit's styled one, so Markdown is not rendered.
on_error_label str "❌" The label shown briefly when the copy fails.
type "primary", "secondary", or "tertiary" "secondary" The button's emphasis, as in st.button. "secondary" is the usual bordered button, "primary" fills it with your theme's primary color, and "tertiary" is plain text with no border or background.
width "content", "stretch", or int "content" The button's width, as in st.button: "content" fits the contents, "stretch" matches the parent container, and an integer is a fixed width in pixels (capped at the container's width). With show_text, the text button takes the extra width.
aria_label str or None None The name screen readers announce. By default the visible label is the name, except the bare "📋", which is announced as "Copy to clipboard". Start a custom value with the visible label text so the two agree.

Keys

You usually don't need one. When key is omitted, a key is generated from the line of code that called st_copy_button, which keeps each button distinct and stable across reruns — including inside @st.fragment and behind conditionals, since one line is one button no matter which runs execute it.

Pass an explicit key when several buttons share a single line, a loop being the usual case, so that changing how many there are — or the order they run in — doesn't renumber them:

for row in rows:
    st_copy_button(row.token, key=f"token_{row.id}")

Keys must be unique across your whole app, the same as any other Streamlit element key.

Requirements

Browsers only expose the clipboard API in a secure context: HTTPS, or localhost. On an app served over plain HTTP from anything else — a LAN address, a container IP, a reverse proxy without TLS — the copy will fail. The button then shows on_error_label for a second, and the call returns False, so you can surface it in your app too:

if st_copy_button(text) is False:
    st.warning("Couldn't reach the clipboard — copy the text manually.")

A None text fails the same way: there is nothing to copy, so the button shows on_error_label and the call returns False. Pass a str to copy something.

Requires Python 3.10+ and Streamlit 1.31+.

Development

poetry install                                       # Python dependencies
cd st_copy_button/frontend && npm install && npm run build   # compile the frontend
poetry run streamlit run st_copy_button/example.py   # run the demo app

The frontend build output is gitignored but is packaged into the wheel, so a fresh npm run build is needed before poetry build.

To work on the frontend with hot reloading, set _RELEASE = False in st_copy_button/__init__.py and run npm start (port 3001) alongside the app.

There are three test suites:

poetry run pytest tests/                                  # Python side, headless via Streamlit's AppTest
cd st_copy_button/frontend && CI=true npm test && cd -    # frontend, Jest in jsdom
poetry run playwright install chromium                    # once
poetry run pytest e2e/                                    # end to end, Playwright against a live app

The end-to-end tests load the compiled frontend, so run npm run build again after changing it. poetry run pytest on its own runs both Python suites.

License

MIT. This project contains portions derived from st-copy-to-clipboard by Sasmitha Manathunga, whose copyright and MIT terms are preserved in LICENSE.

Metadata

Release files for st-copy-button 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for st-copy-button 0.5.0
File Size Uploaded Provenance Actions
st_copy_button-0.5.0.tar.gz 519.5 kB Without provenance Details for st_copy_button-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for st-copy-button 0.5.0
File Interpreter ABI Platform Actions
st_copy_button-0.5.0-py3-none-any.whl Python 3 none any Details for st_copy_button-0.5.0-py3-none-any.whl

Total release size: 1.0 MB

Release files / st_copy_button-0.5.0.tar.gz

Download URL st_copy_button-0.5.0.tar.gz
Size 519.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1898e33d6003925019b9fc24591376a420f524c5beb4e724030e356c6b6eb4ea
BLAKE2b-256 checksum
How to use checksums
39d3f9e83f0102f9bb022cb41e9b0603495320ea2938869eb91b12f1ce04fb94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.12.14 Linux/5.15.154+

Release files / st_copy_button-0.5.0-py3-none-any.whl

Download URL st_copy_button-0.5.0-py3-none-any.whl
Size 523.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6c95f34a0a31220dd967909be909493d0383f1e96068d3d5bed6699e7ed79eb
BLAKE2b-256 checksum
How to use checksums
db2e633618722582c11dec628f26d110d6b8dc3f9d952e8e60d11b18297fba30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.12.14 Linux/5.15.154+

Security for st-copy-button 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page