I want to make a application that run as a web application or desktop application, I know how to implement for a web server using Django or Flask, i prefer Django, but is there any way to port it to a desktop application too?
I found a nodewebkit that maybe give me the solution but, I don`t have any idea on how to use it.
import wx
from wx.lib.iewin import IEHtmlWindow
a = wx.App(redirect=False)
f = wx.Frame(None,-1,"My Desktop Application")
browser = IEHtmlWindow(f)
browser.Navigate("http://google.com")
f.Show()
a.MainLoop()
is a pretty good way for a remote web page to pretend to be a windows application ... assuming thats what you are asking for.. (this uses wxPython so of coarse you will need to install it)
Me personally prefer node-webkit(renamed to nwjs) for this kind of things. Its very easy and powerful, you should give it a try. here are some tutorials.
this,
this and
this. here is the github page nwjs.
but if your background is in python not nodejs then take a look at cefpython
Related
I finally made a project that I wanted to make since a long time :
I'm using an Arduino Uno to replace my PC power button (with a simple relay) and that Arduino Board is connected to a Raspi 3 for network connection purposes
My wish is to do a webpage (or a API-Like request) that at a touch of a button (preferably in a password-protected page) It'll power the PC on
I know how to code in Python, and my script to control the Arduino is already done but I can't find a way to run, only server-side, a Python Script from a button in a webpage
I found that CherryPy framework but I don't think it'll suit my needs
Can someone give me any ideas about that please?
As already mentioned by #ForceBru, you need a python webserver.
If this can be useful to you, this is a possible unsecure implementation using flask:
from flask import Flask
from flask import request
app = Flask(__name__)
#app.route('/turnOn')
def hello_world():
k = request.args.get('key')
if k == "superSecretKey":
# Do something ..
return 'Ok'
else:
return 'Nope'
If you put this in an app.py name file and, after having installed flask (pip install flask), you run flask run you should be able to see Ok if visiting the url http://localhost:5000/turnOn?key=superSecretKey .
You could write a brief html gui with a button and a key field in a form but I leaves that to you (you need to have fun too!).
To avoid potential security issues you could use a POST method and https.
Look at the flask documentation for more infos.
I'm trying to create a simple REST Web Service using technology WSME reported here:
https://pypi.python.org/pypi/WSME
It's not clear, however, how to proceed. I have successfully installed the package WSME.0.6.4 but I don't understand how to proceed.
On the above link we can see some python code. If I wanted to test the code what should I do? I have to create a .py file? Where this file should be saved? Are there services to be started?
The documentation is not clear: it says "With this published at the / ws path of your application". What application? Do I need to install a Web Server?
Thanks.
You could use a full blown web server to run your application . For example Apache with mod_wsgi or uWSGI , but it is not always necessary .
Also you should choose a web framework to work with .
According WSME doc's it has support for Flask microframework out of the box , which is simple enough to start with .
To get started create a file with the following source code :
from wsgiref.simple_server import make_server
from wsme import WSRoot, expose
class MyService(WSRoot):
#expose(unicode, unicode)
def hello(self, who=u'World'):
return u"Hello {0} !".format(who)
ws = MyService(protocols=['restjson', 'restxml'])
application = ws.wsgiapp()
httpd = make_server('localhost', 8000, application)
httpd.serve_forever()
Run this file and point your web browser to http://127.0.0.1:8000/hello.xml?who=John
you should get <result>Hello John !</result> in response.
In this example we have used python's built in webserver which is a good choice when you need to test something out quickly .
For addition i suggest reading How python web frameworks and WSGI fit together
I'm no UI or web designer, though I have made a bunch of simple Tkinter-based GUIs that are simple wrappers to test lower-level code and hardware, such as protocol and data acquisition testers for equipment connected over a serial port or network. Using Python and Tkinter permits my apps to run on every platform supporting Python.
Now I need to migrate my GUIs to support single-user remote access, while still supporting access by local users. And I'd still like the program to be portable across platforms, and even have it be possible to be made into a binary executable (via py2exe, pyinstaller, py2app, etc.)
Are there any toolkits that support Tkinter-like simplicity? Ideally, I'd like to do a line-for-line rewrite to swap Tkinter for something else, rather than reimplement or extensively refactor my apps.
I have found Web2Py and pyjs/Pyjamas, but they seem to be overkill for my simple needs. I also searched for a solution based on a single-instance (or single window) VNC or NX or RDP host, but found nothing applicable.
What is the most direct way to "remote-ify" my Tkinter GUIs?
If I do need to completely dump my Tkinter architecture/code and start over from scratch, what approach would best meet my needs?
I would recommend checking out CherryPy. It's really easy to wrap your head around and get a quick server up and running - it doesn't have the overhead/complexity that a lot of other frameworks impose (Django!!). Unfortunately you will have to rewrite the UI in html, but ultimately it will most likely be worth the effort. Check out Twitter's Bootstrap to get the ball rolling on a quick and attractive UI that just "works".
An example of how concise a CherryPy app can be:
import cherrypy
class SessionExample:
#cherrypy.expose
def index ( self ):
if cherrypy.session.has_key ( 'color' ):
out = "<font color='{0}'>{0}</font>".format(cherrypy.session['color'])
else:
out = ""
return out + ("<form method='POST' action='setColor'>\n"
"Please choose a color:<br />\n"
"<select name='color'>\n"
"<option>Black</option>\n"
"<option>Red</option>\n"
"<option>Green</option>\n"
"<option>Blue</option>\n"
"</select><br />\n"
"<input type='submit' value='Select' />\n"
"</form>"
#cherrypy.expose
def setColor (self, color):
cherrypy.session ['color'] = color
return "Color set to {}".format(color)
cherrypy.config.update({
"server.socketPort" = 8080,
"server.environment" = "development",
"server.threadPool" = 10,
"sessionFilter.on" = True
})
cherrypy.root = SessionExample()
cherrypy.server.start()
Navigate to localhost:8080 in a web browser and you should see a color picker. Simple!
I've created a simple gstreamer-based python audio application with a GTK+ GUI for picking and playing a webstream from a XML list. Then I connected my PC speakers output to the input of an old stereo receiver with large loudspeakers and presto, I have a pretty good sound system that is heard over most of my home.
Now I'd like to add a web user-interface to remote control the application from a room other than the one with the computer but so far all my attempts have been fruitless.
In particular I wonder if it is possible to create a sort of socket with signals like those of GTK GUIs to run methods that change gstreamer parameters.
Or is there a more realistic/feasible strategy?
Thanks in advance for any help!
You could use Bottle, a very simple micro web-framework.
Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.
Hello world:
from bottle import route, run
#route('/hello/:name')
def index(name='World'):
return '<b>Hello %s!</b>' % name
run(host='localhost', port=8080)
The fastest and easiest way would probably be using cgi-scripts. If you want a more sophisticated approach you could consider using a webframework like django, turbogears or the likes.
I would suggest using one of the lighter-weight pure-Python web server options and either write a stand-alone WSGI application or use a micro-framework.
Gevent would be a good option: http://www.gevent.org/servers.html
Here is a sample implementation of a WSGI application using Gevent:
https://bitbucket.org/denis/gevent/src/tip/examples/wsgiserver.py#cl-4
For a micro-framework, I'd suggest using Flask.
How to convert the web site develpoed in django, python into desktop application.
I am new to python and django can you please help me out
Thanks in Advance
I think you should just create an application that connects to the webserver. There is a good answer to getting RESTful API calls into your django application. This means you'd basically just be creating a new front-end for your server.
Using django-rest-interface
It doesn't make sense to rewrite the entire django application as a desktop application. I mean, where do you want to store the data?
For starters, you'll have to replace the web UI with a desktop technology like Tk/Tcl.
If you do that, you may not want to use HTTP as the protocol between the client and the services.
Django is a web framework. If you're switching to a desktop, you'll have to forego Django.
I would try to replicate the Django application functionality with the PyQt toolkit.
You can in fact embed web content in PyQt applications, with the help of QtWebKit. I would post some potentially useful links, but apparently I have too low a reputation to post more than one :)
There are two places you can go to try to decouple the view and put it into a new desktop app. First you can use the existing controller and model and adapt a new view to that. Second, you can use only the existing model and build a new view and controller.
If you haven't adhered closely enough to the MVC principles that you can detach the model from the rest of the application, you can simply rewrite the entire thing. if you are forced to go this route, bail on django and http entirely (as duffymo suggests above).
You have to also evaluate these solutions based upon performance requirements and "heaviness" of the services. If you have stringent performance requirements then relying on the HTTP layer just gets in the way, and providing a simple API into your model is the way to go.
There are clearly a lot of possibly solutions but this is the approach I would take to deciding what the appropriate one is...
It is possible to convert a django application to a desktop app with pywebview with some line of codes. Frist create a python file gui.py in directory where manage.py exists. install pywebview through pip, the write the code in gui.py
import os
import sys
import time
from threading import Thread
import webview
def start_webview():
window = webview.create_window('Hello world', 'http://localhost:8000/', confirm_close=True, width=900, height=600)
webview.start()
window.closed = os._exit(0)
def start_startdjango():
if sys.platform in ['win32', 'win64']:
os.system("python manage.py runserver {}:{}".format('127.0.0.1', '8000'))
# time.sleep(10)
else:
os.system("python3 manage.py runserver {}:{}".format('127.0.0.1', '8000'))
# time.sleep(10)
if __name__ == '__main__':
Thread(target=start_startdjango).start()
start_webview()
then run gui.py with the command python gui.py. IT will create a window like desktop app
There's a project called Camelot which seems to try to combine Django-like features on the desktop using PyQt. Haven't tried it though.
I am thinking about a similar Problem.
Would it be enough to habe a minimal PyQt Gui that enables you to present the djando-website from localhost (get rid of TCP/HTTPS on loop interface somehow) via QtWebkit?
All you seem to need is to have a minimal Python-Broser, that surfs the build in Webserver (and i guess you could even call Django directly for the html-payload without going over the HTTP/TCP layers).
I have django manage.py runserver in .bat file and a localhost bookmark bar in a browser and whola a django-desktop-app. Or make your own browser that opens localhost. Creating a web-browser with Python and PyQT