Flask is Github’s top Web framework for learning Python.

Flask has recently released its 1.0 update, and it’s been eight years since the project was open-source to the latest 1.0 version, Flask.


Flask is Github’s top Web framework for learning Python.

Run Python app.py and visit http://localhost:5000/ to see Hello World! Flask was born eight years ago on April Fool’s Day, and has since become popular. Flask has never released a formal version in eight years, but it remains the most popular Python Web framework on Github; The Flask kernel has two of the most important components built in, and all the other components are integrated through an easily extensible plug-in system. The two built-in components are Werkzeug and Jinja2.


Flask is Github’s top Web framework for learning Python.

The Werkzeug toolkit for writing Python WSGI programs is well loved in the open source community, and its source code is one of the most readable open source libraries in Python technology.


Flask is Github’s top Web framework for learning Python.

Run python wsgi.py, open a browser, visit http://localhost:4000/, and see Hello World!


Flask is Github’s top Web framework for learning Python.

Jinja2 is an extremely powerful templatingsystem that perfectly supports Unicode Chinese, each template runs in a secure sandbox environment, and the template code written in Jinja2 is beautiful.


Flask is Github’s top Web framework for learning Python.

The werkzeug and Jinja2 libraries, together, are good to look at, and Armin Ronacher picked them as a pillar for flask to illustrate his particular code taste. The author is a handsome guy from Australia!

Oh, oh, oh, let’s experience flask’s strange charms!


Flask is Github’s top Web framework for learning Python.

Install the flask

<pre >pip install flask

</pre>

PI calculation API


Flask is Github’s top Web framework for learning Python.

PI can be found using the sum of the reciprocal squares of positive integers, and as the series goes to infinity, the value gets closer and closer to PI.


Flask is Github’s top Web framework for learning Python.

Run Python flask_pi.py, open your browser and visit http://localhost:5000/pi? N =1000000, you can see the page output 3.14159169866, which is now very close to PI.

Note that the return value of PI () cannot be a floating-point number, so STR must be used to convert it to a string

Taking a closer look at the code, you’ll also notice one variable in particular, request, which appears to be a global variable. It is very strange to take the request parameter from the global variable. If you’re in a multi-threaded environment, how do you make sure that each thread gets the same request parameters that the thread is processing? So it can’t be a global variable, it’s a thread-local variable, and thread-local variables don’t look different from global variables on the outside, but when you call thread-local variables, each thread gets a target that’s shared internally by the thread at the time.

Cache accounting results

In order to prevent repeated accounting, we cache the PI (n) value that has been calculated so that we can query it directly next time. Instead of just returning a string, let’s return a JSON string with a cached field to indicate whether the output was directly fetched from the cache.


Flask is Github’s top Web framework for learning Python.

Run Python flask_pi.py, open a browser and go to http://localhost:5000/pi? N =1000000, you can see the page output


Flask is Github’s top Web framework for learning Python.

Why, readers may ask, does the caching class PiCache need to use RLock? This is because Python dictionary reads and writes in multithreaded environments are not completely thread-safe, and locks are needed to protect data structures.

Distributed cache

The cache above is just an in-memory cache, and when the process restarts, the cache results disappear and the next calculation has to start again.


Flask is Github’s top Web framework for learning Python.

If the second port 5001 is enabled to provide service, the second process will not be able to enjoy the memory cache of the first process and will have to recalculate. Therefore, distributed cache Redis is introduced to share computing cache to avoid cross-process repeated computing and restart recalculation.


Flask is Github’s top Web framework for learning Python.

Run Python flask_pi.py, open a browser and go to http://localhost:5000/pi? N =1000000, you can see the page output


Flask is Github’s top Web framework for learning Python.

Restart the process and refresh the page again. The cached output will still be true, indicating that the cached output is no longer lost when the process restarts.

MethodView

Those of you who have written Django might ask whether Flask supports API writing methods for class methods, and the answer is yes. Here we use Flask’s natively supported MethodView to rewrite the above service.


Flask is Github’s top Web framework for learning Python.

We’ve completed the get method for MethodView, illustrating that the API only supports GET methods for HTTP solicits. If you want to support POST, PUT, and DELETE, you need to do it yourself.

Flask’s tacit MethodView is nice to use, but it doesn’t work well either. It doesn’t provide API services with multiple URL names in a single class. So let’s introduce flask’s extension flask-classy to solve this problem.

Flask Extension Flask-classy

The first step is to install the extension PIP install flask-classy, and then add a new API service to the same class to calculate the Fibonacci series.


Flask is Github’s top Web framework for learning Python.


Flask is Github’s top Web framework for learning Python.

Visit http://localhost:5000/fib/100, we can see the page output


Flask is Github’s top Web framework for learning Python.

Well, that’s all for today! Have you all learned it? Welcome to the Python community: 862672474