0

Is there any way in a browser, to type python code into an input field, it will then be sent to a local server and executed and the result pushed back to the browser. Basically a browser hosted python notebook, where the code gets evaluated on a different machine. Is there any python package to do this.

something like what ideone.com or picloud do, but opensource and that can install on your own server.

Or any suggestions on how to do it, I have looked around already but have struggled to find something meaningful.

3 Answers 3

1

That can be done using the http.server module in Python 3. I've posted an example below. Adapt it to your needs.

import http.server
import socketserver

PORT = 8000

class ScriptHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):

    def do_POST(self):
        """ Handle POSTed script """

        try:
            result = eval(self.rfile.read())
            self.send_response(200,'Ok')
            self.wfile.write(result)
        except:
            #handle errors


httpd = socketserver.TCPServer(("", PORT), ScriptHTTPRequestHandler)

print("serving at port", PORT)
httpd.serve_forever()

Once the server is running create a HTML form with an action of "http://localhost:8000/" and it should execute the do_POST method above. Put your HTML files in the same folder or subdirectories of the server script. See the http.server python docs for full details.

Sign up to request clarification or add additional context in comments.

Comments

1

It might be overkill, but you could have a look at Sage: It's a "free open-source mathematics software system", so you'll get lots of mathematical tools, too, but you can still execute arbitrary Python code. You can try it online at http://www.sagenb.org/ – this is also what you can get locally.

Comments

1

I haven't tried myself. You may want to check out ipython notebook.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.