Hacker News new | comments | ask | show | jobs | submit login
Create Python GUIs using HTML (And Webkit and GTK) (aclevername.com)
40 points by skorgu on Apr 30, 2009 | hide | past | web | favorite | 10 comments



Neat idea, but checkout that horrible horrible synchronous message pass to the gtk idle thread ...

    def execute((R, function, args, kwargs)):
        R.result = apply(function, args, kwargs)
        while R.result is NoResult:
            time.sleep(0.01)
        return R.result
ouch.


I was under the impression that time.sleep'ing all over the place was the only way to let threads do their work in python because that's one of the few ways to release the GIL. Is there some other way to get this done (that doesn't involve C modules)?


Instead, use a "message reply" concept. For example, for your data you could send a tuple with a callback or a condition.

Then block on the condition, and have the lambda/delegate in the idle thread signal the condition. Make sure you hold the lock for the conditions mutex before sending the message though so that it is not a race condition.

#Sender

callback = SomeFunc

mutex.lock()

def worker():

    # ran in main

    callback()

    mutex.lock()

    cond.signal()

    mutex.unlock()
send_msg_to_idle(0, worker)

cond.wait()

mutex.unlock()


gtk.threads_enter()

try:

   blah
finally:

   gtk.threads_leave()
or even better and wrap that as a context manager so you do

with gtk_lock():

   blah


and if i wasn't clear, the reason this is bad is because you wake up the cpu every 10ms to check. this is quite bad for power consumption, especially if it is a critical path in your framework.


I came up with my own technique for developing "GUI" without any fancier tool than standard programming languages and their frameworks.

I create a launcher called "weboot" that gets parameters like directory and URL resource to start on. It launches an http server process and sends a command to the browser via command-line (but could use other means) with a generated URL (because you don't want to be stuck on an already used port). Browser goes to: http://127.0.0.1:8066/some_resource

Now you have HTML+JavaScript for the frontend and your preferred web framework and tools on the backend. It all costs just as much as you use, so don't launch Apache, just a small http server will do. ;-)

The secret comes next. The frontend needs to ping the backend every several seconds to keep it from shutting down on purpose. The reason is that after the user closes the browser or the tab, the server process has done its job and should close automatically. It all works very well on Linux. I have created a small calculator and a tetris game to test this already and I pompously called them widgets. ;-)

I also have my own ideas for creating an IRC client based on this technique as I have created several versions of IRC clients over the years and even though XChat works great for me on Linux so I don't have a real urge to do that again, it's a trivial exercise in the same way the tetris game was. When I get my bugs list under control I might give it a go again.

So, the JavaScript side loops with something like:

setInterval(function(){ new AjaxRequest('/ping'); }, 5000);

These values are all tunable. I started with a ping every 5 seconds and the server closes in 12 seconds (two pings + some extra time), but I pulled those numbers out of thin air and in 12 seconds I can even reopen a tab fast enough to avoid the server dieing in case of trouble. ;-)

I like Ruby rather than Python, and I have used GTK+ with Ruby for a while myself, but the GTK+ development in Ruby has been a little under trouble as it seems as if no one who is good enough at C and Ruby has it as a high priority on their list of things to maintain, with more and more things moving to the web and so on...

Also, even the development of GTK+ itself and Gnome in general seem to be under a little trouble as more developers seek greener pasture elsewhere and new blood is rarer to come by to replace them. Things like QT, Swing, SWT (Eclipse's UI toolkit?), and even Gtk# (Mono) are generally on another planet than low level C required by GTK+.

Good luck.


Awesome! I remember when I was first learning how to program and I wanted to make a gui for a simple m3u playlist manager I had written, but tkinter was too much for me at the time. I explicitly remember thinking "I wish I could just use html and css instead of this crazy stuff" since I already knew how to make static web sites. (I got in to programming by making personal webpages, then started with javascript, and from there moved to python. I expect this is how most young people start programming now.)

This is awesome. I am going to have to try this out later tonight.


Excellent idea! Desktop based GUIs should move towards HTML/JS/CSS like platforms. I think the biggest advantage of something like this is the ability of desktop utilities to be cross-platform and integrate better with web based and other desktop applications. Adobe AIR is a move in this direction. Adobe received around 1M downloads of AIR within the first few weeks (But its still too 'flashy' for my tastes (would personally prefer something like this)).


Clever but why not use Titanium App? You can use Ruby and Python.

See http://www.appcelerant.com/titanium-preview-release-pr3.html .


Took me a moment to work out this means 'Creating HTML / Webkit / GTK GUIs using Python'




Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: