dccClipboard

The dccClipboard component copies text to the
user’s clipboard with a single click.

Examples

Find a few usage examples below.

Simple Clipboard Example

The easiest way to trigger the copy is by using the property.
No callback is required! Place dccClipboard in the layout where
you would like the copy icon located. Specify the of the component
with text to copy. In this example, the content of the value property of the
dccTextarea is copied to the clipboard.

This example has not been ported to R yet - showing the Python version instead.

Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21

from dash import Dash, dcc, html

app = Dash()

app.layout = html.Div([
    dcc.Textarea(
        id="textarea_id",
        value="Copy and paste here",
        style={"height": 100},
    ),
    dcc.Clipboard(
        target_id="textarea_id",
        title="copy",
        style={
            "display": "inline-block",
            "fontSize": 20,
            "verticalAlign": "top",
        },
    ),
])

if __name__ == "__main__":
    app.run(debug=True)

Clipboard Icon inside a Scrollable Div

The style and className can be used to change the design or the position
of the copy icon. This example shows the icon placed in the top right corner
of a scrollable div.

This example has not been ported to R yet - showing the Python version instead.

Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21

from dash import Dash, dcc, html

app = Dash()

markdown = """
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Cras non lacus maximus,
tincidunt nibh in, finibus nisi. Cras
ut lacus sed lacus tempus rutrum. Integer
ut feugiat nisi, nec tempus velit.

Proin placerat erat odio, et laoreet
sapien mattis et. Curabitur laoreet imperdiet
congue. Integer congue a augue non vulputate.
Ut a leo auctor, sodales sem ac, cursus
ipsum. Fusce in est nec urna pretium
aliquet. Nunc nisl eros, blandit eu diam
convallis, elementum dictum tortor.
"""

app.layout = html.Div([
    dcc.Markdown(
        markdown,
        id="copyable-markdown",
        style={"width": 500, "height": 200, "overflow": "auto"},
    ),
    dcc.Clipboard(
        target_id="copyable-markdown",
        style={
            "position": "absolute",
            "top": 0,
            "right": 20,
            "fontSize": 15,
        },
    ),
], style={"width": 500, "height": 200, "position": "relative"})


if __name__ == "__main__":
    app.run(debug=True)

Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Cras non lacus maximus,
tincidunt nibh in, finibus nisi. Cras
ut lacus sed lacus tempus rutrum. Integer
ut feugiat nisi, nec tempus velit.

Proin placerat erat odio, et laoreet
sapien mattis et. Curabitur laoreet imperdiet
congue. Integer congue a augue non vulputate.
Ut a leo auctor, sodales sem ac, cursus
ipsum. Fusce in est nec urna pretium
aliquet. Nunc nisl eros, blandit eu diam
convallis, elementum dictum tortor.

Updating the Clipboard Text in a Callback

When is not specified, the content of the text property
is copied to the clipboard. This works well with components like the DataTable
where you may want to customize the text in a callback. In this example,

This example has not been ported to R yet - showing the Python version instead.

Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21

from dash import Dash, dcc, html, Input, Output, State, dash_table, callback
import pandas as pd

app = Dash()

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/solar.csv")

app.layout = html.Div(
    [
        dcc.Clipboard(id="table_copy", style={"fontSize":20}),
        dash_table.DataTable(
            df.to_dict("records"),
            [{"name": i, "id": i} for i in df.columns],
            id="table_cb",

        )
    ]
)


@callback(
    Output("table_copy", "content"),
    Input("table_copy", "n_clicks"),
    State("table_cb", "data"),
)
def custom_copy(_, data):
    dff = pd.DataFrame(data)
    # See options for .to_csv() or .to_excel() or .to_string() in the  pandas documentation
    return dff.to_csv(index=False)  # includes headers

if __name__ == "__main__":
    app.run(debug=True)

Limitations

This component uses theClipboard API. This feature is available only in secure contexts
(HTTPS), in some or all supporting browsers. When
the Clipboard API is unavailable, the icon will not appear in the app and a warning message is
written to the console.

Currently dccClipboard only supports copying text to the
clipboard. It does not support paste or other clipboard operations.


Clipboard Properties

Our recommended IDE for writing Dash apps is Dash Enterprise’s
Data Science Workspaces,
which has typeahead support for Dash Component Properties.
Find out if your company is using
Dash Enterprise
.

id (character; optional):
The ID used to identify this component.

target_id (character | named list; optional):
The id of target component containing text to copy to the clipboard.
The inner text of the children prop will be copied to the clipboard.
If none, then the text from the value prop will be copied.

content (character; optional):
The text to be copied to the clipboard if the target_id is None.

n_clicks (numeric; default 0):
The number of times copy button was clicked.

html_content (character; optional):
The clipboard html text be copied to the clipboard if the target_id
is None.

title (character; optional):
The text shown as a tooltip when hovering over the copy icon.

style (named list; optional):
The icon’s styles.

className (character; optional):
The class name of the icon element.

loading_state (named list; optional):
Object that holds the loading state object coming from dash-renderer.

loading_state is a named list with keys:

  • component_name (character; optional):
    Holds the name of the component that is loading.

  • is_loading (logical; optional):
    Determines if the component is loading or not.

  • prop_name (character; optional):
    Holds which property is loading.