How to get user-written code from a CodeEditor and run it?

I want to use pn.widgets.CodeEditor(...) with the following requirements:

  • The user of the panel can type Python code
  • After the user is done typing their code, there is a “Run” button that executes the code.

Is this possible? How would I do this?

I imagine something like this, but potentially dangerous if the users are not trusted.

code_editor = pn.widgets.CodeEditor(...)

eval(code_editor.value)
1 Like

depending on the use case you might panel convert the app to make it running directly in the browser using pyodide. Then you would not put the server at risk.

Alternatively you might run the code in a virtual environment different from the one the server is running in. Or inside a separate docker container.