Option to read data from clipboard

Dear Team,

I would like to understand if we have a functionality to copy data from clipboard similar to pandas read from clipboard option.

My intended use case is as follows -

  1. User can copy a part of excel sheet (just few rows and columns)
  2. Select a button to directly read from clipboard
    or
  3. Paste it in a text area
  4. Used the copied data (with formatting retained) to read it as pandas dataframe (maybe by using pd.read_clipboard)

Let me know if there is a way to achieve this using streamlit or any possible workaround.

Thanks

1 Like

I dont think capturing navigator data is supported in streamlit yet.

You can try building a custom component for it perhaps.
The logic would be on a event, capture navigator.clipboard.readText(), wrap it with StringIO and pass it to pd.read_csv with separator of your choice. ( by default pd.read_seperator is pd.read_csv with \s+ I guess )

https://alligator.io/js/async-clipboard-api/ <- some js side reference.

PS. I am working on a streamlit component that exposes the navigator apis eg. geo, battery, clipboard etc. , will have something available (unstable) around the weekend :slight_smile: you would be able to do,

import pandas as pd
from io import StringIO

clip_data = stnav.clipboard.get_text()
pd.read_csv(StringIO(clip_data), sep="\\s+")

with that probably. and Most welcome to this awesome community !

1 Like

Update.
After streamlit 0.73.0 this code snippet works with streamlit-bokeh-events 0.1.2

import streamlit as st
from bokeh.models.widgets import Button
from bokeh.models import CustomJS
from streamlit_bokeh_events import streamlit_bokeh_events
from io import StringIO
import pandas as pd


copy_button = Button(label="Get Clipboard Data")
copy_button.js_on_event("button_click", CustomJS(code="""
    navigator.clipboard.readText().then(text => document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: text})))
    """))
result = streamlit_bokeh_events(
    copy_button,
    events="GET_TEXT",
    key="get_text",
    refresh_on_update=False,
    override_height=75,
    debounce_time=0)

if result:
    if "GET_TEXT" in result:
        df = pd.read_csv(StringIO(result.get("GET_TEXT")))
        st.table(df)

Hope it helps!

1 Like

Nice: @ash2shukla an you help us here : Copy dataframe to clipboard - #5 by Capi_hacendado

1 Like