Using Django minus the web server - python

I'm writing a syndication client, with the aim being to have a client for devices, and a web site that has the same functionality. I shall develop the website using Django - this is already decided; the client shall be written in python with both a CLI and a PyQt4 GUI. I have been writing the clinet first, and it's fairly database-heavy, as everything is cached to enable it to be read while offline.
It struck me today that it would make sense to use Django models for my application, to reduce the repetition of effort between the client and the website. My question is how easy it is to seperate this, and how much of Django I will need in my client to use Django's models. AFAIK I should not need to run the server, but what else is needed? I had an idea of generating the same html for my client as the website, but showing it withing Qt widgets rather than serving pages for a browser.
Has anyone tried this sort of thing before? I'm starting on this already, but it would be good to get a warning of potential dead-ends or things that will create a maintainance nightmare...

Read up on standalone Django scripts and you'll be on your path to victory. Basically all you're really doing is referencing the Django settings.py (which Django expects) and then using models without web views or urls.
If all you're really interested in is using Django's ORM to manage your models and database interaction, you might want to consider using SQLAlchemy instead.

You'll still have to run the Django app as a web server, but you can restrict it to serve to only localhost or something. And sure, you can use QtWebKit as the client.

Related

Should a background process be part of Django project?

I'm trying to build a web app which provide an interface to do some queries upon data extracted from another public API server.
To complete the queries in real time, I would have to prefetch the data from the public API server.
So I think it is reasonable to separate the app deal with query input(there is still some logic here, so only javascript wouldn't be enough) from the app which runs in background and builds the database which could possibly answer the query in real time.
Then what comes to my mind is does this background app really have to be a part of Django project? It runs almost without interacting with any Django component. Except for the database which is also accessible by a Django app and some signals maybe(when to start/stop collecting data, but this could probably also decided internally).
What would a be good design choice for my situation?

managing server application

I have a server which runs flask with python.
Now I want to make an application which can do various tasks like uploading files, updating redis database and various other things.
Now ofcourse this could be done using html pages but since the operation could involve lots of files realtime input of data and other things it might be better to make an application and manage the server from that point rather than webpages.
do you suggest using webpages anyway or would you make an application for it?
and if I make an application should I use http or not?
sorry if this is a uninformed question but I would like to learn the best methods
You might want to look into Flask-Script. It allows you to run various commands related to your flask application easily. It also allows you to easily add your own commands to it. This way you will be able to keep your administrative code still within the Flask app, but not necessarily have it accessible via a web page.

maintaining user authentication if i have some web pages on mod_python and some on mod_wsgi

I have a web application written in raw python and hosted on apache using mod_python. I am building another web application which is django based and will be hosted on same server using mod_wsgi.
Now, the scenerio is such that user will login from the web page which is using mod_python and a link will send him to my application which will be using mod_wsgi. My question is how can I maintain session? I need the same authentication to work for my application.
Thanks in advance.
If you're using django with mod_wsgi and a raw python page which only serve a link to django application, you don't need to maintain session on both page. When user click on first link and reach the django application, just check session there.
Django have session_db which use memcache. More information can be found here:
Django Sessions
SSO across web applications is poorly supported. One thing you can look at is:
http://www.openfusion.com.au/labs/mod_auth_tkt/
What you can do is really going to depend though on what authentication database you are currently using in the mod_python application and how you are remembering that someone is logged in. If you can provide that information, may be able to suggest other things.
Conceptually: store a cookie using your raw python web page that you process in a "welcome" view or custom middleware class in Django, and insert them into the sessions db. This is basically what hungnv suggests.
The most ridiculous way to do this would be to figure out how Django deals with sessions and session cookies, insert the correct row into Django's session database from your raw python app, and then custom-set the session cookie using Django's auth functions.

Deciding to WSGI or Django for new web app

I'm in the process of setting up a new web app and deciding whether to just do it with WSGI or go the full framework route with Django.
The app's foremost requirements:
1) The app has no UI what so ever and all of the data is exposed to clients via a REST api with JSON.
2) It will have data to persist so MongoDB & probably Amazon's SimpleDB will be used for the database side.
Is there a reason to use Django or can I get marginal speed improvement with WSGI only?
Previous server-side apps I've built were either with Java/Struts and Groovy/Grails on the JVM. My understanding is that Django is an MVC framework similar to Rails and Grails.
I've also played around with Google App Engine which uses WSGI as thin layer above your code for managing and routing requests.
I suggest you consider something between those two extremes. Flask is lightweight, very easy to use, and connects to your web server via wsgi. You can use regular python database connectors with it, and a few databases even have Flask-specific extension modules.
I have worked with Django for a couple of projects and I like it a lot, but since you are going to use mongoDB and a lot of JSON I suggest you use NodeJS as server side, with Express as framework, you can see a brief tutorial here:
http://howtonode.org/express-mongodb
One of the advantages of this is that you will use only javascript all along your project, I began working with this technology the last month in a Hackathon, and I can tell you that I'm very impressed of how fast and simple it is.
I've worked a bit with some django "apps" ,its really easy, but setting up the "apps" can be a bit of a long process. Django has a lot of nice features that you won't be using and I agree that you might be on one "extreme" here.

HTTP framework for a python based server serving mobile applications

I am working on building a python powered back-end server for a mobile application. I am using MongoDB hosted on the Cloud for storage. The communication between the Application and the server will be mostly JSON. There wont be any HTML involved in this transfer as such.
I am looking for a framework to build my server. I did some research and found (django, Pylons etc) are for full stack server and is not needed for my requirements.
I am confused on how to go about selecting an HTTP framework (web.py, cherrypy etc).
also, i might use Mongoengine as the ODM. I am not sure if this decision is anyway going to affect the framework selection
any ideas?
I'd recommend bottle or flask, both of which are very minimal and impose no constraints on you whatsoever. In the case of bottle, it's particularly easy to build a simple RESTful server since it will automatically return JSON from a route that returns a dict:
from bottle import route, run
#route('/user_info/:id'):
def user_info(id):
user = get_user(id) # ...do something, lookup user, whatever
return {'first_name': user.first_name, 'last_name': user.last_name}
run()
Pyramid Framework doesnt enforce a templating engine (not a full stack) or a persistence/storage system. It has json and jsonp renderer (no html at all).
It also has a scaffold for jquery mobile app (I havent tried this, so I dont know).
I personally prefer web.py, but in your case it is possible to start with basic wsgi app, or something simple as http://werkzeug.pocoo.org/ because it doesn't enforce a specific template engine or database adapter.

Categories