How to create plugins for a web application - python

I am developing web applications in python using Django framework. I would like to develop some plugins to extend the functionality of the app. Can anybody tell me how to start about it as i have found nothing specific for web apps in python.
And also can the plugins be developed in any language or should they be in python as well??
Plugin: Suppose I am writing a parser to parse different forms of data, then i want each plugin to take care of each type of data. They should be included only if required by the user. The user has to download the plugin and be able to work on it.
Thanks,
Harsha

If you're talking about how to create a package that other Python developers can import into their application, whether it be a Django project or not, and have it installable via easy_install or pip, you need to register for an account on PyPi and follow the tutorial to get started.
If you want to write your "plugin" in a language other than Python, and offer that functionality on the web, regardless of language or platform, you need to create a Web Service to accept input and return output, usually in XML or JSON format.
Working with a Web Service is NOT like working with a Python package. There is nothing to download or install, there are simply URL endpoints that respond to HTTP verbs.

If you would like to make scripts that parse data and then fill your database with it then check Django documentation on how to write custom django-admin commands: https://docs.djangoproject.com/en/1.5/howto/custom-management-commands/
Such commands will use django env and will have access to your models.

Related

adding browser capability to an existing python script

In essence I want to run existing python scripts through a web browser, display the text and plots(if any) and also keeping the ability to run them through the command line.
I am wondering is there any toolkit that can help me with the development.Also it would be nice if the toolkit does or allows JavaScript based interactive plots.
Thanks!
-Abhi
WSGI is designed for just this purpose - it provides an interface for a web server to initiate python scripts.
You probably don't want to work with WSGI in the raw. Flask is a straightforward, simple framework you might use for this.
The details of how to actually build a WSGI web server are well beyond the scope of a stackoverflow answer - you can find plenty of tutorial docs on Flask's website.

Using pouchdb with python

I am doing some research on taking a python app written in either django or web2py, and using it as a desktop app. And for a database, remotely I will be using couchdb for it's replication abilities. For the dektop, I need to find a way to integrate pouchdb into the app, but short of the homepage saying to include the pouchdb.js file, I can't find any docs on how to use it with python. And how to get setup in general for localhost to remote server syncing.
I was hoping someone could shed some light on how to get this accomplished. As for django/web2py, im not sure which one I want to use yet but have narrowed it down to those 2.
The idea for the app itself is that I have the website which has all the normal functionality available. And the user can install a package locally to their desktop from an installer. That local copy would have a subset of features from the website and sync some data from the pouchdb instance locally, to the remote server. enough to be able to fully interact with the db when offline.
For this purpose I created Python-PouchDB a few days ago. It runs PouchDB inside QtWebKit via either PyQt4, PyQt5 or PySide (you need to ship one of them with your app), and provides a nice (both synchronous and asynchronous) Python interface.
You can install it by executing:
pip install python-pouchdb
(or just by extracting the pouchdb directory from the source which you can find linked on the website.)
Example usage:
>>> import pouchdb
>>> environment = pouchdb.setup()
>>> db = environment.PouchDB("test")
>>> db.put({"_id": "mytest"})
{u'ok': True, u'rev': u'...', u'id': u'mytest'}
>>> db["mytest"] #shorthand for db.get()
{u'_rev': u'...', u'_id': u'mytest'}
>>> db.destroy()
PouchDB runs on IndexedDB in the browser, with future plans for supporting WebSQL, but in general the idea is the it builds on local database capabilities provided by the browser.
If I understood right you want to be able to access it from Python? I don't think that's going to be possible, while finding a Python wrapper around a JavaScript engine might be possible, but you wouldn't be able to access the browser backed database.
You should also consider making a CouchApp instead, though that still won't give you the ability to use Django templates.
Basically the way you should structure your app is that it only uses JavaScript based templating (Mustache, Handlebars, etc etc) and interact with your API using a REST interface, so you don't have to have a Python engine running for the client side offline app. It might still be doable, but if you have to bundle tons of technologies with your app it will be a heavy download.

RESTful Web service or API for a Python program in WebFaction

I have developed a few python programs that I want to make available online.
I am new to web services, and I am not sure what I need to do in order to create a service where somebody makes a request to an URL (for example), and the URL triggers a Python program that displays something in the user's browser, or a set of inputs are given to the program via browser, and then python does whatver it is supposed to do.
I was playing with the google app engine, which runs fine with the tutorial, and was planning to use it becuase it looks easy, but the problem with GAE is that it does not work well (or does not work at all) with some libraries that I plan to use.
I guess what I am trying to do is some sort of API using my WebFaction account.
Can anybody point me in the right directions? What choices do I have in WebFaction? What are the easiest tools available?
Thank you very much for your help in advance.
Cheers
Well, your question is a little bit generic, but here are a few pointers/tips:
Webfaction allows you to install pretty much anything you want (you need to compile it / or ask the admins to install some CentOS package for you).
They provide some default Apache server with mod_wsgi, so you can run web2py, Django or any other wsgi frameworks.
Most popular Python web frameworks have available installers in Webfaction (web2py, django...), so I would recommend you to go with one of them.
I would also install supervisord to keep your service running after some reboot/crash/problem.
I would be glad to help you if you have any specific question...

Modify system configuration files and use system commands through web interface

I received a project recently and I am wondering how to do something in a correct and secure manner.
The situation is the following:
There are classes to manage linux users, mysql users and databases and apache virtual hosts. They're used to automate the addition of users in a small shared-hosting environnement. These classes are then used in command-line scripts to offer a nice interface for the system administrator.
I am now asked to build a simple web interface to offer a GUI to the administrator and then offer some features directly to the users (change their unix password and other daily procedures).
I don't know how to implement the web application. It will run in Apache (with the apache user) but the classes need to access files and commands that are only usable by the root user to do the necessary changes (e.g useradd and virtual hosts configuration files). When using the command-line scripts, it is not a problem as they are run under the correct user. Giving permissions to the apache user would probably be dangerous.
What would be the best technique to allow this through the web application ? I would like to use the classes directly if possible (it would be handier than calling the command line scripts like external processes and parsing output) but I can't see how to do this in a secure manner.
I saw existing products doing similar things (webmin, eBox, ...) but I don't know how it works.
PS: The classes I received are simple but really badly programmed and barely commented. They are actually in PHP but I'm planning to port them to python. Then I'd like to use the Django framework to build the web admin interface.
Thanks and sorry if the question is not clear enough.
EDIT: I read a little bit about webmin and saw that it uses its own mini web server (called miniserv.pl). It seems like a good solution. The user running this server should then have permissions to modify the files and use the commands. How could I do something similar with Django? Use the development server? Would it be better to use something like CherryPy?
Hello
You can easily create web applications in Python using WSGI-compliant web frameworks such as CherryPy2 and templating engines such as Genshi. You can use the 'subprocess' module to manadge external commands...
You can use sudo to give the apache user root permission for only the commands/scripts you need for your web app.

Installing Sphinx on App Engine - possible?

Following up on my last year's question on documentation, I now want to get started and try out Python-based Sphinx for putting together the developer documentation for a PHP CMS I've been working on.
Instead of setting up Python locally on my workstation, I would like to run it on a publicly accessible web server from the start. All the web hosting packages I have access to run on the LAMP stack, and I'm reluctant to buy Python-based hosting. I am very interested in the Google App Engine, the free quotas they provide will do for me a hundred times over, and even if not, their pricing looks very reasonable.
Now I have zero knowledge of Python - getting Sphinx to work would be my first contact with it - and very little time. As far as I understand, the platform and python libraries the App Engine provides are very compatible to a standard Python library but not identical.
So my question is:
Can Sphinx run on App Engine at all?
Is installing Sphinx on the App Engine as straightforward as if I would install it on top of a normal Python installation? Or will the App Engine's environment require tweaking of the source code that I can't perform in reasonable time with my current level of Python?
Should I be installing Sphinx on a local server and a "normal" Python stack instead first?
Does anybody know any helpful How-to's, tutorials or other resources for this?
You do not need to install Sphinx on GAE at all.
You use Sphinx to create a directory of static HTML, CSS and JS. When this step is finished, you simply upload the output from Sphinx -- in it's entirety.
The output from Sphinx (HTML, CSS and JS) is simply served from one place. You upload the documentation from where you created it to GAE as static files and serve them. Done.
There's no "install on a web server" aspect to Sphinx at all. Sphinx is not a web application; it does not run on your web server. You run it in development briefly to publish the documentation to HTML, LaTeX or whatever. Once you've created the static HTML files, you no longer need any part of Sphinx anywhere.
Can Sphinx run on App Engine at all?
I suppose it can, but you never need to.
Is installing Sphinx on the App Engine
as straightforward as...
It doesn't matter, because you don't install it there. You install it on your workstation and use it on your workstation.
Should I be installing Sphinx on a
local server and a "normal" Python
stack instead first?
Don't install it on a server. Install it on your workstation.
Does anybody know any helpful
How-to's, tutorials or other resources
for this?
If by "this" you mean "installing on a web server", then then answer is "no". One does not install it on a web server. So there are no how-to's, tutorials or resources for "installing on a web server".
If by "this" you mean "creating documentation with Sphinx", then the answer is "what's wrong with https://www.sphinx-doc.org? What do they lack?
I would like Sphinx's "engine" that
turns the input (consisting of reST
files) into HTML/CSS/JS to be
accessible from anywhere to make me
(and possible other contributors)
independent from a specific
workstation.
Sphinx is like a compiler. Everyone has their own copy on their workstation. They download the document source, make changes, commit the source changes, and upload the resulting document.
serve the generated documentation from
the same place.
Correct. After you download the source, make changes, regenerate the document and commit the changes, you upload the resulting document so it will be served from one -- and only one -- place.
Sphinx is a compiler. It is not a "web engine". It's a simple compiler that simply compiles your documentation into HTML so it can be served.
Interesting project! The main issue you're going to run into is that of filesystem access: The filesystem on App Engine is read-only, and based on looking at the source, Sphinx is fairly hard-coded to use the filesystem for output. It also expects to read the configuration file and input files from the local filesystem, which would make building docs for anything other than projects bundled with the app tricky.
It is possible to work around this, but it would require writing a simple virtual filesystem that uses the App Engine datastore, and using monkeypatching to make it work with the regular Python file interface. That's rather advanced for a "new to Python" project!
One other thing you might want to consider if you were pondering allowing users to upload projects to be documented: The Sphinx configuration file is in Python, so executing it could be dangerous - a user could do nasty things to your app with a malicious configuration file!
The difference between deployment locally via dev_appserver and remotely via appcfg has been - in my experience - limited to which of the two commands I execute. That said, I've no experience with Sphinx.
Sphinx compiles documentation, running it on a webserver makes as much sense as running gcc on a webserver.

Categories

Resources