i am learning python with GAE and for interface use jquery so i want to know calling python function using jquery.
jQuery runs in the browser, operates on the DOM tree and gets data from servers mostly through asynchronous HTTP requests. Python runs on the backend - in case of GAE on Google's infrastructure. So the way to call functions on the server from the client is by making requests.
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
All you need for this to work is something on the server responding to a GET request to ajax/test.html - be it a static file or a Python function (e.g. a RequestHandler), which handles the request and returns some data.
Use AJAX!!
Define a webapp page using
class AjaxPage(webapp.RequestHandler):
def get(self):
result = your_function()
self.response.out.write(str(result))
And call it through the page's url through ajax.
I believe you need to create a route for the app engine, and call that route with jQuery.
--EDIT-- The below turns out to be really really untrue, so don't mind it!!! I'm not going to remove it 'cause that would be lame. I have done some work with the Python SDK and tiny bit with the Java SDK, and doing so I felt the Python SDK was falling behind. More experienced programmers seem to disagree, hence this edit.
Have to warn you though, Google is focussing more on the Java platform than the Python platform; very often extra work is involved to achieve the same results as with Java, and sometimes things are simply unsupported.
What is your focus here? To learn Python or to learn Python with GAE?
Related
I have a API server which is written in python.
And I wanted to render my react components on server side.
So I searched the best practices and result was having a nodeJS server.
But in my thoughts, this is weird because I have to build a another server with different language. This is bad at maintainable, and maybe have an overload.
So I want to know having nodeJS server is usual. and also how do big companies do server-side rendering.
If it is as simple as wanting to use Python with React, you can use python-react to render React components.
As noted above, if you are using Flask, the templating is in Jinja2, but that isn't necessarily mutually exclusive to using React in your project.
This resource on using Flask/React may be of use to you, as well.
I am building a web app where I'd like to use GWT for generating the java script that runs in the browser but because I use other python packages (specifically scipy) I would like for the web server to be python (not a servlet engine). Has anybody done so ? Any pointers would be appreciated.
Thank you.
Ranga
GWT is a front-end framework. You can use any backend technologies, whether it is Python, PHP, Java, Node.js or any Server-side technologies. There is no limitations on this. I haven't use GWT for quite long now, You have the request builder to fetch data from the server.
Anyway, the simplest solutions that gonna scale for wide range of your devices is to build a REST API on the backend with python, its very simple, you have many simply python technologies like Bottle or Flask that does it in a giffy for you. Build your backend and make it follow REST conventions.
Then, on the level of GWT, it has to have something for fetching data from the webserver. I'll advise to make your data in JSON and then you'll be able to use technologies like GSON to convert your JSON serialised string into Java objects on the fly
Pyjamas looks like it was originally a port of GWT to Python:
https://wiki.python.org/moin/Pyjamas
This guy doesn't like it though, so you might consider the cons to doing this as well:
http://blog.pyjeon.com/?p=302
I'm working on a web interface which currently runs using PHP and communicates locally to a python script.
I'm moving the web side to appengine, which so far is going well when being used locally, I'm currently communicating from the appengine app to the python app via get requests that are handled by the python script.
The problem is, that obviously the machine running the python script will be behind a firewall, I've never needed to do this before and am not sure on how to implement this best.
The only idea I have so far is for the python script to send post requests to the appengine with some data and then as a response, send back some other data. The only problem with this is that the web interface should update the client quite fast.
Any ideas?
Take a look at ProtoRPC Python API: https://developers.google.com/appengine/docs/python/tools/protorpc/overview
Though it is still marked as experimental, it seems to be a decent framework for what you are trying to do - send messages back and forth between the apps.
Since you said your local app runs behind a firewall, I'm assuming you cannot open up an endpoint and protect it with some form of authentication.
Once you have messages flowing, you can either use Channel API to keep the front-end updated: https://developers.google.com/appengine/docs/python/channel/overview
Or if you want to go more basic, just implement long/short polling through AJAX.
Sorry with the limited amount of info you have provided, that's all I can think of right now. Please feel free to post more details and I'll try to help further.
Disclaimer: I'm not very familiar with any of the things mentioned in the question title.
Would it be possible to use a browser control (like Webkit) as a frontend for a WSGI app (using a framework like Flask) without starting a local WSGI server?
Basically the requests and responses are managed by a middle layer between the HTML UI and the WSGI backend. A certain URI could mean "Local", for instance "local://" or something similar, and will be routed to the embedded WSGI app with all the original headers etc.
You will lose any features that a normal WSGI server provides unless you implement it yourself or somehow embed a server that is also usable via an API instead of real HTTP requests.
Now that I think of it, this is the only real requirement: A WSGI server that is callable via an API and not just real HTTP requests.
I know the usefulness of this is questionable (and maybe doesn't even make sense). My question is whether this is at all possible?
EDIT: Here's another way of putting it:
I want a single codebase to be both a web app and a desktop app, using an HTML frontend and a Python backend. I don't want to run a server on any port for the desktop app. What's the easiest way to achieve this?
It is in theory possible to write your own WSGI container that implements a full API and adapts that to WSGI. flup might bring some inspiration.
Earlier today I saw exactly what you're asking for -- a way to call WSGI through an API without actually connecting over the network. However, it shouldn't be that hard.
On a side note, you might want to look at PySide, of particular interest to you may be the ability to bind python elements to DOM events, so if you're just looking to trigger python code that's an even shorter route.
If you give some more detail on what you're hoping to achieve we might be able to dial it in for you.
Reviving this, since we're facing the same problem and are about to scale things up from a single view/widget to the whole app.
What I did was to simply set the base URL to something where I serve static content, and from a QRC file that's easy:
html = jinjatemplate.render(...)
self._mainFrame.setHtml(html.decode('utf-8'), Qt.QUrl('qrc:///Orsync/html/'))
For the communication, our HTML uses AJAX over jQuery for most things. You could wrap that in a layer that either does $.post(...) or api.post(...) like this:
self._mainFrame.addToJavaScriptWindowObject('api', self._webapi)
You'd need to decode the URL and create a request object yourself, but maybe that's not too hard to do? We use very few URLs currently (who are mapped directly to python objects/functions) so it's easy to do the mapping ourselves.
Data that goes back is just sent using QMainFrame.evaluateJavaScript(...), either as a direct Qt call or as a bunch of code lines fetched using $.getScript(...) (which just evaluates the code received).
I'm currently rebuilding things a bit using CherryPy, and it maps urls -> Python objects straight off, so I'm hoping there's something to be gained by that.
Otherwise, I would wish one could run QWebKit over named pipes or something similarly localized and not a tcp-socket. :)
I'm quite new in Python world. I come from java and ABAP world, where their application server are able to handle stateful request.
Is it also possible in python using WSGI?
Or stateful and stateless are handled in other layer?
Usually, you don't work with "bare" WSGI. You work with web-frameworks, such as Pylons or TurboGears2.
And these contain a session-middleware, based on WSGI - called "Beaker". But if you work with the framework, you don't have to worry about that - you just use it.
But if you insist, you can of course use Beaker standalone.
I prefer working directly on wsgi, along with mako and psycopg.
It's good to know about Beaker, though I usually don't hold state in the server because I believe it reduces scalability. I either put it in the user's cookie, in the database tied to a token in the user's cookie, or in a redirect url.
Your question is a little vague and open-ended. First of all, WSGI itself isn't a framework, it's just the glue to connect a framework to the web server. Secondly, I'm not clear on what you mean when you say "state" -- do you mean storing information about a client on the server? If so, web frameworks (Pylons, Django, etc) allow you to store that kind of information in web session variables.