Can I use python3 classes inside a Flask app? - python

I'm designing a Flask app that graphs some weather data for several cities. It makes sense to me to use a "City" class that handles the fetching and parsing of the data every time the page is loaded. However, what I'm not sure about is how Flask would handle these instances. Is Flask "smart" enough to know to release the memory for these instances after the page is served? Or will it just gradually consume more and more memory?
Alternatively, would I just be able to create a single global class instance for each city OUTSIDE of the "#app.route" functions that I could use whenever a page is requested?
The deployment server will be Windows IIS using FastCGI, in case that matters at all.

Flask is "just" a framework. It is still executed and managed by the "normal" Python interpreter so the question "how Flask would handle these instances" is nonexistent.
Define classes and use their instances as you would in any other Python project/snippet, however it might be beneficial to think where to define them.
It will not make sense inside a route since the class will be redefined every time a request is received, but the how is exactly the same.

Related

Bulk getEditResponseUrl() for Shared Forms with Python

What we'd like to do is iterate over all shared forms on a Google account (potentially hundreds), over which we'd like to, using Python, then call the getEditResponseUrl() function for each FormResponse, which seems only accessible via Google Apps Script, which we could presumably just call in Python using execute() per https://developers.google.com/apps-script/api/quickstart/python.
However, it seems like the Apps Script with this behavior would need to be included in each shared form first, else the function in question won't exist as far as the client library is concerned; is there a way to call, for example, getEditResponseUrl() and other functions that are part of FormResponse and FormApp objects using any kind of Python client library or HTTP API, rather than having it exclusively found in the context of Google Apps Scripts and explicitly defined in the form beforehand? Thanks!

How to turn a simple python script to basic webapp?

I would like to know what is the fastest way to turn a simple Python script into a basic web app.
For example, say I would like to create a web app that takes a keyword from the user and display the most retweeted tweet on Twitter. If I write a python script that is capable of performing that task using Twitter's API, how would I go about turning it into a web app for people to access?
I have looked at frameworks such as Django, but it would take me weeks or months to learn how to use it. I just need something quick and simple. Any such alternatives?
Make a CGI script out of it. You basically get the request information from the webserver via environment variables and you print the desired HTML to stdout. There are helper libraries such as Werkzeug which help with abstracting away the handling of the environment variables by wrapping them in a Request object.
This technique is quite outdated and isn't normally used nowadays as the script has to be run on every request and thus incurs the startup cost all the time.
Nevertheless this may actually be a good solution for you because it is quick and every webserver supports it.

Why do Python MVC web frameworks use views.py to contain route functions?

I've developed many applications using the MVC pattern in Zend and Symfony. Now that I'm in Pythonland, I find that many frameworks such as Flask, Django and Pyramid use a file called views.py to contain functions which implement the routes. But, these "views" are really controllers in other MVC frameworks I've used before. Why are they called views in Python web frameworks? And, can I change them to controller.py without tearing a hole in the Python universe?
A view, from the django's perspective is what content is presented on a page. And the template is the how it is presented.
A django view is not exactly a controller equivalent. The controller in some of those other frameworks is how the call of a function happens. In django, that is a part of the framework itself.
Technically, there is nothing preventing you from renaming your views into controllers.- The URL routing scheme takes either the function or the string to the function. As long as you can send the appropriate string to the function (or the function itself), you can call your view whatever you want. However, for the reason stated in the paragraph above and for the fact of meeting the expectations of the other people that work on django, you should not really have files called controller.py.
It's just a matter of getting used to. Hang in there for a bit.

Where is the best place to put one-time and every-time code in GAE/Python?

I am new to Google App Engine and Python. I am having trouble understanding some basic questions about Python apps running on Google App Engine.
If I want code to execute:
On every incoming request, where should I put it? We are capturing session information about what pages are viewed when by whom, and what did they do, etc.
Only once when the app is started, where should I put it? I need to initialize a number of application/system variables that are used in many places in the application. Where is the best place to put the code to do this?
If anyone can point me to any documentation or tutorials that explain what the best architecture practices are for GAE/Python apps, without the basics of programming stuff, that would be great.
Question Number 1:
Some web frameworks (Django, KAY, etc) have a concept of Middleware. You can create your own middleware that will execute on every request and handle this sort of information
(see: https://docs.djangoproject.com/en/dev/topics/http/middleware/)
Question Number 2:
Warmup requests (see: https://developers.google.com/appengine/docs/python/config/appconfig#Warmup_Requests)
Though since warmup requests are not guaranteed to run, you can put a global variable to let the instance know if it was "initialized" and check that variable on every page load (this will be cheap since the variable will live in memory and exist between requests). If it is not set, then run through your "warmup" as needed.

Elegant way to make RPC Calls to Python Functions in a Django Project

I'm in the middle of trying to create a django website to access data in a MySQL database. The intenion is to also create a UI in Dojo (javascript). Also I would like the django backend to also provide webservices (RPC for python functions) to allow access to the MySQL database remotely. So for example, if someone wants to use Perl scripts to access the database (and possible other additional functionality like calculations based off of data in the database) they can do so in their native language (Perl).
Now ideally, the web services API is the same for javascript as well as another remote service that wants to access these services. I've found that JSON-RPC is a good way to go for this, as there is typically built in support for this in javascript in addition to the numerous additional benefits. Also a lot of people seem to be preferring SOAP to JSON.
I've seen several ways to do this:
1) Create a unique URI for each function that you would like to access:
https://code.djangoproject.com/wiki/JSONRPCServerMiddleware
2) Create one point of access, and pass the method name in the JSON package. In this particular example an SMD is automatically generated.
https://code.djangoproject.com/wiki/Jsonrpc
The issue with (1) is that if there are many functions to be accessed, then there will be many URI's that will be used. This does not seem like an elegant solution. The issue with (2) is that I need to compare functions against a list of all functions. Again this is not an elegant solution either.
Is there no way that we can take the advantages of (1) and (2) to create an interface such that:
- Only one URI is used as a point of access
- Functions are called directly (without having to be compared against a list of functions)
Any help with this will be really appreciated. Thanks!
what about using REST API?
One possibility to do the comparisons would be to use a dict like so:
def func1(someparams):
#do something
return True
def func2(sameparams):
#do something else
return True
{'func1': func1,
'func2': func2}
Then when you get the API call, you look it up in the dict and call from there, any function not in the dict would get the 404 handler.
It sounds like what you really want is a RPC server of some kind (SOAP, say, using soaplib) that is written in python and uses your application's data model, and what ever other APIs you have constructed to handle the business logic.
So I might implement the web service with soaplib, and have it call into the datamodel and other python modules as needed. People wanting to access your web application's data would use the SOAP service, but the web application would use the underlying datamodel + apis (for speed, your web app could use the SOAP service too, but it would be slower).

Categories

Resources