1

How can I include a Python terminal / console in a Django application that users can execute python code against? For example, df.column_b * df.column_a if the example data is:

column_a, column_b
1,        2
2,        4
3,        8

My use case is that it is easy for me to get data into my Django application. However, a common use case is for users to copy and paste the data into a spreadsheet and start doing some work / computation with the data. It would be nice if they didn't have to leave the page and could just do the computations directly in Python.

An example website is LearnPython.org. After inspecting the website a little bit, it looks like it uses CodeMirror.js. I have taken a look at the CodeMirror docs and the div for LearnPython.org's Run button, but it is not clear to me how I'd take the leap from registering an onClick event to:

  • Sending the code to the Django server
  • Executing the code
  • Sending the results back

Or could I do this all client side?

Any pointers in the right direction is greatly appreciated.

Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • You shouldn't do this. It creates huge security holes in your application. You should never allow arbitrary code from a client to run on the servers. It'd be trivial for them to destroy your servers, or steal sensitive data. – NDevox Mar 01 '19 at 15:29
  • @NDevox - Thank you for the advice. This is for an internal application that would only be at our company. But also, LearnPython.org does this, so surely the security risks can be handled? Do you think it is trivial for you to destroy LearnPython.org's servers right now? – Scott Skiles Mar 01 '19 at 16:07
  • Take a look to http://www.brython.info/ or http://www.skulpt.org/. – albar Mar 01 '19 at 16:20
  • Thanks @albar. And re-reading your comment NDevox, it does make sense not to let code execute on your servers. I think client-side is definitely the safe approach. – Scott Skiles Mar 01 '19 at 16:35
  • @albar - Do you want to answer with those two links and make a note about how one should not do this server side? I saw this also: https://stackoverflow.com/questions/3068139/how-can-i-sandbox-python-in-pure-python – Scott Skiles Mar 20 '19 at 20:24

1 Answers1

0

Have you seen this project?

What is DataCamp Light, and how can I use it?

In addition to an extensive course library, DataCamp also offers 'Open Courses' that any DataCamp user can create and take. If you already have MOOC (Massive Open Online course) content you would like to publish using the DataCamp learning environment, you can get started here: https://www.datacamp.com/teach and see some examples here: https://www.datacamp.com/community/open-courses.

Community
  • 1
  • 1