Spaces:
Running
Running
File size: 2,032 Bytes
a9340d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
from pathlib import Path
from typing import Optional
import streamlit as st
import streamlit.components.v1 as components
# Tell streamlit that there is a component called streamlit_copy_to_clipboard,
# and that the code to display that component is in the "frontend" folder
frontend_dir = (Path(__file__).parent / "frontend").absolute()
_component_func = components.declare_component(
"streamlit_copy_to_clipboard", path=str(frontend_dir)
)
def st_copy_to_clipboard(
text: str,
before_copy_label: str = "📋",
after_copy_label: str = "✅",
show_text: bool = False,
key: Optional[str] = None,
theme: str = 'light', # default theme is 'light'
):
"""
Streamlit component to copy text to clipboard.
Parameters
----------
text : str
The text to be copied to the clipboard.
before_copy_label : str
Label of the button before text is copied.
after_copy_label : str
Label of the button after text is copied.
show_text: bool
If True, show text right before the button and make it clickable as well
key : str or None
An optional key that uniquely identifies the component.
theme: str
Set the current theme for the button.
"""
component_value = _component_func(
key=key,
text=text,
before_copy_label=before_copy_label,
after_copy_label=after_copy_label,
show_text=show_text,
theme=theme,
)
return component_value
def main():
st.write("## Example")
text = st.text_input("Enter text to copy to clipboard", value="Hello World")
st_copy_to_clipboard(text)
st_copy_to_clipboard(text, before_copy_label='📋Push to copy', after_copy_label='✅Text copied!')
st_copy_to_clipboard(text, before_copy_label='Push to copy', after_copy_label='Text copied!', show_text=True)
st_copy_to_clipboard(text, before_copy_label='Push to copy', after_copy_label='copied!', show_text=True, theme="dark")
if __name__ == "__main__":
main()
|