How to implement copying to the clipboard in Python?

I don’t understand how it is possible to implement copying to the clipboard on the server. I run the FLET application, everything works locally, but when I test it on the server, it doesn’t work.

I tested both pyperclip and page.set_clipboard - no result.

@handle_exceptions(alert_success_msg=f"Link copied to clipboard")
def copy_to_clipboard(self, e, text: str):
  self.page.set_clipboard(text)
  pyperclip.copy(text)

I understand that the machines are launched in isolation and because of this they do not have access… But somehow it is possible to implement this.

Server log:

Unexpected error occurred: 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
On Linux, you can run `sudo apt-get install xclip` or `sudo apt-get install xselect` to install a copy/paste mechanism.

Help, if anyone has encountered such a problem or knows a solution, I will be very grateful. :persevere:

have you tried installing xclip or xselect as suggested in the log?

yes but it didn’t help

I’m not certain what you’re doing with the server side here, but clipboards (and xclip/xsel) are utilities to work with X11’s clipboard, for graphical desktop user environments. These would be leveraged by a user running your app locally on a Linux desktop. A server environment usually doesn’t have a display server or graphical output, unless it’s one that you’re using as a remote desktop. Web apps use javascript for this – I don’t know how Flutter does it, much less how Flet does it. Reading very briefly on their docs, I’m wondering if pyperclip.copy() is actually redundant. Does set_clipboard() not do the same thing?

Yes, indeed, pyperclip.copy() and set_clipboard() work the same.
Apparently the only solution is to use javascript. :confused:

From what I’ve seen so far, set_clipboard() should work, if not bugged. Since this is a cross-platform framework for native and web apps, I expect this to execute the right javascript on the web site anyway. Might be worth opening an issue on the flet github project, or contributing to an existing one.