[Python] Copy to clipboard from Linux Dash server to windows client

Discussão em 'Python' iniciado por Stack, Novembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    Have anyone have success with creating a linux Dash server where it is possible to copy to the clipboard of windows client?

    I have tried:

    pd.DataFrame(data).to_clipboard()


    this gives

    Traceback (most recent call last):
    File "/app/smb_shares/python_lib/ppds/ppds_dash/dashboard.py", line 410, in copy_to_clipboard
    pd.DataFrame(data).to_clipboard()
    File "/app/smb_shares/python_env/pyenv/versions/3.10.12/envs/base_3.10.12/lib/python3.10/site-packages/pandas/util/_decorators.py", line 333, in wrapper
    return func(*args, **kwargs)
    File "/app/smb_shares/python_env/pyenv/versions/3.10.12/envs/base_3.10.12/lib/python3.10/site-packages/pandas/core/generic.py", line 3241, in to_clipboard
    clipboards.to_clipboard(self, excel=excel, sep=sep, **kwargs)
    File "/app/smb_shares/python_env/pyenv/versions/3.10.12/envs/base_3.10.12/lib/python3.10/site-packages/pandas/io/clipboards.py", line 178, in to_clipboard
    clipboard_set(text)
    File "/app/smb_shares/python_env/pyenv/versions/3.10.12/envs/base_3.10.12/lib/python3.10/site-packages/pandas/io/clipboard/__init__.py", line 659, in lazy_load_stub_copy
    return copy(text)
    File "/app/smb_shares/python_env/pyenv/versions/3.10.12/envs/base_3.10.12/lib/python3.10/site-packages/pandas/io/clipboard/__init__.py", line 316, in __call__
    raise PyperclipException(EXCEPT_MSG)
    pandas.errors.PyperclipException:
    Pyperclip could not find a copy/paste mechanism for your system.
    For more information, please visit
    https://pyperclip.readthedocs.io/en/latest/index.html#not-implemented-error


    I have tried to install

    sudo apt-get install xclip


    On the server, but that did not change anything - the error is still there. From what I can see pandas does not support copying cross-platfrom. Or maybe I am missing something?

    My next step was to look at Dash's build in dcc.clipboard.

    #%%
    import dash
    from dash import dcc, html, Input, Output, State
    import pandas as pd

    # Initialize the Dash app
    app = dash.Dash(__name__)

    # Sample function to generate a DataFrame (can be customized)
    def generate_dataframe():
    # You can customize this function to generate any DataFrame
    df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
    })
    return df.to_csv(index=False)

    # App Layout
    app.layout = html.Div([
    # Button to trigger the DataFrame generation and copy action
    html.Button("Generate and Copy DataFrame", id="generate-copy-button", n_clicks=0),

    # Hidden TextArea to store the DataFrame content before copying
    dcc.Textarea(
    id="data-text",
    style={"display": "none"}, # Hidden TextArea
    value="" # Initially empty
    ),

    # Clipboard component to copy the content
    dcc.Clipboard(
    target_id="data-text", # Targeting the hidden TextArea
    title="Copy to clipboard",
    id="clipboard",
    style={"display": "none"} # Hidden, since the button triggers it
    ),

    # Message to display after copying
    html.P(id="copy-message", style={"color": "green", "fontWeight": "bold"})
    ])

    # Callback to generate the DataFrame and copy it to clipboard when the button is clicked
    @app.callback(
    [Output("data-text", "value"), # Update the TextArea with the DataFrame content
    Output("copy-message", "children"), # Update the message after copying
    Output("clipboard", "n_clicks")], # Trigger the clipboard component
    [Input("generate-copy-button", "n_clicks")], # Button click input
    prevent_initial_call=True
    )
    def generate_and_copy(n_clicks):
    # Generate DataFrame content
    df_content = generate_dataframe()

    # Update the message
    copy_message = "DataFrame content copied to clipboard!"

    # Trigger the clipboard action by setting n_clicks to 1
    return df_content, copy_message, 1


    if __name__ == '__main__':
    app.run(port=8041,debug=True)

    # %%


    In this example it works from linux server -> linux client , but not linux server --> windows client ( nothing is put on the clipboard)

    Continue reading...

Compartilhe esta Página