【问题标题】:How to create a copy-to-clipboard button in streamlit?如何在streamlit中创建复制到剪贴板按钮?
【发布时间】:2022-06-22 04:02:56
【问题描述】:

This discussion 提供了一种解决方法:

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

text_to_be_copied = "Some text"
copy_dict = {"content": text_to_be_copied}

copy_button = Button(label="Copy Text")
copy_button.js_on_event("button_click", CustomJS(args=copy_dict, code="""
    navigator.clipboard.writeText(content);
    """))

no_event = streamlit_bokeh_events(
    copy_button,
    events="GET_TEXT",
    key="get_text",
    refresh_on_update=True,
    override_height=75,
    debounce_time=0)

但是,该按钮看起来与通常的流光按钮不同,不能像这样使用:

if st.button("Some button"):
    st.write("Some reaction")

有没有办法修改流光按钮,以便将文本复制到剪贴板?

【问题讨论】:

    标签: python button clipboard streamlit


    【解决方案1】:

    你必须使用pyperclip库来实现你想要的。

    import pyperclip
    text_to_be_copied = 'The text to be copied to the clipboard.'
    pyperclip.copy(text_to_be_copied)
    

    【讨论】:

      猜你喜欢
      • 2011-06-21
      • 2013-02-05
      • 2010-09-23
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多