I am working on an app that uses OCR on uploaded images. Most of the images will be screenshots taken by the user. Is there anyway the user could simply create the screenshot then ctrl + v it into the file_uploader? This would prevent the extra step of opening paint, saving/cropping the file, and then uploading.
Theres is no inbuilt Streamlit functionality for this. For now they will need to save the file locally.
Although, if they use the select screen shot they wont need to do the cropping step. You can let them know about how to do this in the instructions on how to use your app!
On a mac is command-shift-4 and on windows I believe its windows-shift-s
Hey @pomkos, As @Marisa_Smith said there’s no inbuilt way to do it in streamlit. But there sure is a way if you can use a custom component. streamlit-bokeh-events can do this for you.
This piece of code,
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)
I have taken an example of converting clipboard data to dataframe but you can do whatever you want with it. result.get("GET_TEXT") will give you the text.
Now that’s an awesome code snippet! Unfortunately I cannot reproduce it though. The button was created, however I don’t think it actually does anything (result variable is always None). Other than bokeh and the steamlit_bokeh_events is anything need to be installed on my end? I tried running both remotely and locally but no luck yet.
Edit, a slight modification and it works great! Unfortunately it looks like Safari and Firefox don’t allow this type of functionality, Chrome works fine though. Well, it works great locally, but for some reason the same exact code breaks when I deploy it.
import streamlit as st
from bokeh.models.widgets import Button
from bokeh.events import ButtonClick
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(ButtonClick, 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)
st.write(result.get("GET_TEXT"))
if result:
if "GET_TEXT" in result:
df = pd.read_csv(StringIO(result.get("GET_TEXT")))
st.table(df)
Hey @pomkos!
It will work for Firefox and Safari as well,.you just need to modify the navigator.clipboard part a little. I didn’t have anything else than chrome installed so couldn’t do this. This solution will only work for streamlit >= 0.73.0
Before it we had sandboxed components. Please verify that you’re using streamlit >= 0.73
I will try to find the cross platform code for navigator.clipboard part.
Good stuff it works!
Originally I used pd.read_clipboard() but it does not work on a Heroku app.
But I cannot position it neatly with me streamlit column layouts…
I wish basic clipboard i/o in browser could be supported by the official streamlit framework.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.