Python desktop software with web interface - python

I am building desktop software with a Python backend and a web interface. Currently, I have to start the backend, then open up the interface in the browser. If I accidentally refresh the page - then that clears everything! What I'd like to do is start the application and have a fullscreen browser window appear (using Chrome) - that shouldn't be difficult. I have two questions:
Can refresh be disabled?
Is it possible to hook into closing my program when the web UI is closed?
Update:
What I'm looking for is more like this: geckofx. A way to embed a Chrome webpage in a desktop app. Except I'm using Python rather than C#

Your first question is a dup of disable f5 and browser refresh using javascript.
For your second question, it depends on what kind of application you're building, but generally, the answer is no.
If you can rely on the JS to catch the close and, e.g., send a "quit" message to the service before closing, that's easy. However, there are plenty of cases where that won't work, because the client has no way to catch the close.
If, on the other hand, you can rely on a continuous connection between the client and the service—e.g., using a WebSocket—you can just quit when the connection goes down. But usually, you can't.
Finally, if you're writing something specifically for one platform, you may be able to use OS-level information to handle the cases that JS can't. (For example, on OS X, you can attach a listener to the default NSDistributedNotificationCenter and be notified whenever Chrome quits.) But generally, you can't rely on this, and even when you can, it may still not cover 100% of the cases.
So, you need to use the same tricks that every "real" web app uses for managing sessions. For example, the client can send a keepalive every 5 minutes, and the server can quit if it doesn't get any requests for 5 minutes. (You can still have an explicit "quit" command, you just can't rely on always receiving it.) If you want more information on ways to do this, there are probably 300 questions on SO about it.

Instead of embeding Chrome, you may embed only Webkit ( I don't know on Windows, but on Mac and Linux is easy).
Application logic seams to be on server side, and browser used only as interface. If that is the case, you may put „onbeforeunload” in body tag, and call a js function that send an ajax request to server to die.

Related

Tornado: Websocket connection limit

I am developing a web application with Tornado and have encountered the following problem:
I can't run more than 6 instances of my application in one browser probably because each instance creates websocket connection to Tornado server. I use standard WebSocketHandler class. They close properly, i.e. if I close the 6th tab, then I'd be able to open another application tab.
Is there any way to circumvent it? I will provide any additional information if needed.
EDIT: Connection information (I have 6 identical tabs here, 7th won't load):
Are you sure the limitation is not on the browser? I've seen the same issue (long-polling requests, 7th or 8th won't load), but opening the URL in another browser or location works fine.
Edit: each browser has indeed a limit of simultaneous persistent connections per server, as well as global limit. See this question, and especially this response which has more up-to-date values.

How do I use a browser as a client in python sockets?

I tried to search in the internet for this subject, But I didn't found some answers.
If some know how can I use a browser as a client in python sockets it will be very good.
To use the browser as a client to a python (server) socket, you simply need to point it to the right endpoint.
Assuming you are running the browser and the python server on the same machine, and that you're opening port 1234 on the server socket, you simply need to open the localhost:1234 URL in your browser.
Of course, what happens next is entirely dependent on how you handle the communication in your program. Most browsers will understand plain text put directly on the socket, but you probably want to talk HTTP.
It's worth mentioning that using a plain socket to communicate with a browser is, at best, uncommon. There may be better solutions, depending on what, exactly, you want to do:
If you just want to quickly serve a few files from a directory (i.e.: often
called a "directory listing"), you can use SimpleHTTPServer
If you're trying to build a website, you should look into a web framework, such as Django, Flask or CherryPy
If you want a lower-level highly asynchronous scalable communication, Tornado is a popular choice
You might want to consider using websockets. They essentially function like regular TCP sockets, but are initiated with a HTTP handshake, making them suitable for browsers. They are supported in recent versions of all major browsers. There are many libraries available that adapt common python webservers to serve websockets as well, for example:
https://pypi.python.org/pypi/gevent-websocket/
if you like gevent.
They also support an SSL layer, which is called using a url starting with "wss://" on the browser side. More information here:
https://www.websocket.org/

PyQt cross-platform way of communicating between running applications

Hey guys/ladies of python mastery, need some help.
I'm using PyQt to create some integrated ui elements for an application written in python (appA) that runs on windows/linux/osx, I need those elements to be able to communicate with a PyQt app (appB) I have written that runs separately (same host).
What would be the best (cross-platform) approach to creating a communication link between the integrated ui elements in one app and the standalone app i have written?
I want to be able to send/receive messages from appA to appB...
For example appA launches, integrated ui elements load in a form of an input field with a 'Send' button. Upon entering data and hitting 'Send' a test is performed to check if appB is running/needs to be launched, after appB launches, data arrives at appB and appB sends a confirmation of delivery back to appA and vice versa. This needs to work with least delay and hackery on windows/linux/osx.
I was looking at DBus but that looks a little buggy for WIN, win32api is good for WIN but useless elsewhere, maybe there is a magic bullet to these things.
Any links to tutorials/sites/docs would be great or if u have some ready code :) ! whatever , thanks in advance...
If you don't want to use raw sockets, you should try zmq (zeromq). You can find a good introduction to zmq here

Monitor the Download process in Chrome

I am trying to hack together a Python script to monitor ongoing downloads in Chrome and shut-down my PC automatically after the download process closes. I know little JavaScript and am considering using the PyJs library, if required.
1) Is this the best approach? I don't need the app to be portable, just working.
2) How would you identify the download process?
3) How would you monitor the download progress? Apparently the Chrome API doesn't provide a specific function for it.
Nice question, may be because I can relate with the need of automating the shutdown. ;)
I just googled. There happens to be an experimental API but only for the dev channel as of now. I am not on a dev channel to try that out, so I just hope I am pointing you in the right direction.
One approach would be:
Have a Python HTTP server listening on some port XYZ
To your extension add the permission to the URL http://localhost:XYZ/
In your extension, you could use:
chrome.downloads.search(query, function (arrayOfDownloadItem){ .. })
Where, query is an instance of DownloadQuery, and contains state property as in_progress
You could probably check for the length of arrayOfDownloadItem.
If its zero, create a new XMLHttpRequest to your HTTP server end point, and then let the server shutdown your machine.
HTH

What mutex/locking/waiting mechanism to use when writing a Chat application with Tornado Web Framework

We're implementing a Chat server using Tornado.
The premise is simple, a user makes open an HTTP ajax connection to the Tornado server, and the Tornado server answers only when a new message appears in the chat-room. Whenever the connection closes, regardless if a new message came in or an error/timeout occurred, the client reopens the connection.
Looking at Tornado, the question arises of what library can we use to allow us to have these calls wait on some central object that would signal them - A_NEW_MESSAGE_HAS_ARRIVED_ITS_TIME_TO_SEND_BACK_SOME_DATA.
To describe this in Win32 terms, each async call would be represented as a thread that would be hanging on a WaitForSingleObject(...) on some central Mutex/Event/etc.
We will be operating in a standard Python environment (Tornado), is there something built-in we can use, do we need an external library/server, is there something Tornado recommends?
Thanks
I'm looking into the best options for developing a chat application and was looking into tornado as well. This rough cuts Building the Realtime User Experience has a chapter on building a chat application with tornado that might be useful to you. Best of luck :)
Tornado has a "chat" example which uses long polling. It contains everything you need (or actually, probably more than you need since it includes a 3rd-party login)

Categories

Resources