Python Desktop Application with the Browser as an interface?

Question:

I want to create an application that runs on the users computer, a stand-alone application, with installation and what-not, but I want the interface to be a browser, either internal and displayed as an OS window or external accessible using the browser (i.e. some http server).

The reason would be because I know a little about Python, but I think I can manage as long as I have some basic roots that I can use and manipulate, and those would be HTML, CSS, and Javascript.

I’ve yet to find a good GUI tool which I can use, and always abandon the idea after trying to mess around and eventually not getting anything.

Asked By: Eli

||

Answers:

You could use Pyjamas. It’s a port of Google Web Toolkit to Python, which basically means you write in Python and it gets compiled to HTML and JS.

Answered By: Matthew Flaschen

There are plenty of excellent GUI tools for the way you want to do your GUI — HTML, CSS, and Javascript. If you don’t know of any, ask in a separate question with the right tags.

The Python side in such an arrangement should have no GUI of its own, but just run a subclass of the Python’s standard library’s HTTP server, just serving the HTML, CSS, and JS files, and data via JSON on other URLs that the JS can reach with Ajax techniques, essentially implementing storage and business logi — so it’s far from obvious what “GUI tool” you could possibly want for it?!

Just develop the Python side on its own (e.g. with IDLE, Wingware, SPE, or whatever you like) and the HTML / CSS / Javascript separately, with its own “GUI tool”. All that Python will do with those files is statically serve them, after all.

You could be thinking of using some Python side templating, such as Mojo &c, but my recommendation is to avoid that: rather, go with the “thin server architecture” all the way, make the Python side a RESTful server of business logic and storage layers, and do all the GUI work in the browser instead.

Answered By: Alex Martelli

Python offers two things that should be of your interest:

  • a web server in the standard library
  • a standartized interface for web applications, called WSGI

So it is relatively easy to add a web interface to your application. For example in Mercurial (the versioning system), you have a command hg serve that launches a web server.

To see python launching a web server, and a WSGI app, just do:

python -m 'wsgiref.simple_server'

You can look at the wsgiref source code or some WSGI tutorial to do a simple app.

After that, you may want to use a web framework (for templating & co), but that is another question…

Answered By: Piotr Lesnicki

Are you resorting to a web browser only because you’ve had difficulty with Python widget toolkits, like Tkinter,wxpython, and pyqt?

Have you tried Qt Designer? It’s a graphical GUI designer, making it very quick and easy to develop great looking GUI’s. It gets installed automatically with PyQt.

http://www.riverbankcomputing.co.uk/software/pyqt/download

Answered By: user297250

A simple gui demonstrating how to talk from the browser with python:

sqr.py

import http.server

class TestHandler(http.server.SimpleHTTPRequestHandler):

    def do_POST(self):
        """Handle a post request by returning the square of the number."""
        length = int(self.headers.get_all('content-length')[0])
        data_string = self.rfile.read(length)
        x = float(data_string)
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.flush_headers()
        self.wfile.write(str(x**2).encode())

server = http.server.HTTPServer(("", 8004), TestHandler)
server.serve_forever()

sqrt.html

<body>
<label for="fname">input x:</label>
<input type="text" id="fname" name="fname" onkeyup="runbuttonfunc()" value="4.0"><br>
<button id="runButton" onclick="runbuttonfunc()">=</button><br>
<label for="lname">output x*x:</label>
<input type="text" id="lname" name="lname"><br>

<script type="text/javascript">

function xml_http_post(url, data) {
    var req = new XMLHttpRequest();
    req.open("POST", url, true);
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            document.getElementById('lname').value = req.responseText
        }
    }
    req.send(data);
}

function runbuttonfunc() {
    xml_http_post("sqr.html", document.getElementById('fname').value)
}
</script>
</body>

Run the program with python3 sqr.py and use the address http://localhost:8004/sqr.html in the browser.

The example is derived from How to implement a minimal server for AJAX in Python?.
http.server is a built-in module of python. Yet, there should are many other tools around.

Answered By: Friedrich
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.