Python Desktop Application with the Browser as an interface?
Asked Answered
L

5

26

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.

Ludwigshafen answered 10/4, 2010 at 1:45 Comment(0)
R
12

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...

Rahman answered 10/4, 2010 at 2:6 Comment(1)
For completeness it should be mentioned that running the backend as a localhost wsgiref http server means that it is available to any user who is logged in to the machine not just the user who launches the server. If you are the only user it's not a problem of course; perhaps it could even be regarded as a feature if there are multiple users.Radiotelegraphy
T
3

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.

Tamasha answered 10/4, 2010 at 1:50 Comment(1)
Except it isn't, since its latest commit was only a couple of weeks ago.Menderes
T
2

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.

Truett answered 10/4, 2010 at 1:54 Comment(2)
This is one way, but other languages (e.g. .NET ones) have libraries that allow easy embedding of a browser into a window and scripting it or communicating via a simple API. I've found that useful on several occasions myself, but have been unable to find an equivalent in Python. wxPython has a browser widget, but it's quite outdated and doesn't even support scripting IIRC. Tk has nothing. There's a Webkit embedding for one of the GUI toolkits, but it's a severe pain in the behind to compile and integrate.Atul
I think I'm looking for what Max is talking about, an embedded browser that can communicate directly via JS API with the desktop application.Ludwigshafen
L
0

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

Levona answered 10/4, 2010 at 4:0 Comment(2)
Yes and no. I do have a problem with the GUI toolkits, and for me, as a web developer, it's much easier to get going with standard HTML and JavaScript. Isn't Qt only works for non-commercial applications if I don't have Qt license?Ludwigshafen
@Eli, Qt has a dual LGPL/Commercial license. Basically, if you plan on publishing your application, you either need to open source it, or get a commercial license.Levona
T
0

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.

Transitory answered 18/10, 2020 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.