从 Linux Dash 服务器复制到剪贴板到 Windows 客户端

问题描述 投票:0回答:1

有人成功创建了一个可以复制到 Windows 客户端剪贴板的 Linux Dash 服务器吗?

我已经尝试过:

pd.DataFrame(data).to_clipboard()

这给了

  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

我已尝试安装

sudo apt-get install xclip

在服务器上,但这没有改变任何东西 - 错误仍然存在。 据我所知,pandas 不支持跨平台复制。或者也许我错过了什么?

我的下一步是在 dcc.clipboard 中查看 Dash 的构建。

#%%
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)

# %%

在此示例中,它可以从 linux 服务器 -> linux 客户端运行,但不能从 linux 服务器 -> windows 客户端运行(剪贴板上没有放置任何内容)

python linux plotly cross-platform clipboard
1个回答
0
投票

我在通过 SSH 从 Windows 连接到 Linux 服务器时遇到了同样的问题。我找到的唯一解决方案是使用

pd-replicator

from pd_replicator import replicator

replicator(pd.DataFrame(data), native=False)

我必须将

False
传递给
native
参数才能使“复制”按钮正常工作。一旦出现,单击它即可复制数据框并粘贴到本地计算机上的任意位置。

© www.soinside.com 2019 - 2024. All rights reserved.