I'm doing a python class this semester, and I'd like to add graphical user interfaces to my programs in the form of web pages. Partly I can't be bothered to learn Tkinter, partly I'm just challenging myself, and partly I just like coding interfaces in HTML/JS.
I know the basics on creating HTTP servers with SimpleHTTPServer, but what I need is a way for my web pages to fire ajax commands to python scripts on the server, which will then be executes server side, and then receive the script output. Kind of like how Sage does things.
How can I create an extension for SimpleHTTPServer that I can then use to serve up the outputs of python scripts? I need a solution that's very general, so that given any python script I write, I can easily wrap it in some sort of interface, put it in the server's folder, and not have to do anything else but call it with AJAX, and my server will handle the rest.
My current idea is to have a ServerSideScript
class that my scripts can extend, with a single function called output
. Every script I have should contain a subclass of ServerSideScript named Script. When the server is asked to serve, for example, foo.py, it notices the extension and does something like:
if self.path[-3:]: == ".py":
return getScriptOutput(self.path)
...
def getScriptOutput(self, path):
from path import Script # obviously not going to work, since path is a string
return Script().output()
That can't work for the reason pointed out in the comment, I'm aware of things like the import_module function, but importing seems like an ugly way to do this to begin with.
What's recommended?