It is a reading memo when reading the following books. It takes about 15 hours. (Of which, work and survey around environment construction takes about 3 hours) As I mentioned, I got the impression that it is a framework that is extremely lightweight, can be developed quickly, and is easy to expand.
--Book name: A book that understands Flask from scratch --Author: Takatomo Honda --Published date: Unknown (around July 2018?) --Number of pages: 214 pages --Remarks: Kindle version only
--Uninstall Python that was installed with brew
- $ brew uninstall python
- $ brew uninstall python3
--Uninstall Python that was installed with the official installer -How to uninstall official Python installed on Mac
--Pyenv setup -Building a Python environment on Mac using pyenv-Qiita
--Python installation by pyenv, version specification, operation check
- $ pyenv install -l
- $ pyenv install 3.8.2
- $ pyenv rehash
- $ pyenv global 3.8.2
- $ pyenv versions
- $ which python
- $ python -V
- $ python2 -V
- $ python3 -V
--Installation and operation check of pip
- $ pip install --upgrade pip
- $ pip -V
- $ pip3 -V
--Pipenv setup (you can build a dedicated environment for each project and manage packages)
- $ pip install pipenv
- $ pipenv --version
--From now on, install various packages and execute commands after entering the virtual environment with $ pipenv shell
.
--You can check the packages installed in the virtual environment with `` `$ pipenv graph```
--MacOS comes with Python 2.7 by default
--The entity is under /System/Library/Frameworks/Python.framework
(it cannot be deleted because it is used in the system)
--In addition, macOS Catalina also includes Python 3 (version as of May 2020 is 3.7.3)
--The entity is / usr / bin / python3
(also cannot be erased)
--Looking at / usr / bin /, all `python``` and
`python2series commands have symbolic links to Python 2.7 mentioned above. --The
python3``` command only calls the binary that is placed directly, not the symbolic link.
$ ls -la /usr/bin/python*
lrwxr-xr-x 1 root wheel 75 10 8 2019 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 82 10 8 2019 /usr/bin/python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 75 10 8 2019 /usr/bin/python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 75 10 8 2019 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 82 10 8 2019 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 76 10 8 2019 /usr/bin/pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x 1 root wheel 76 10 8 2019 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
-rwxr-xr-x 1 root wheel 31488 3 18 00:42 /usr/bin/python3
--By the way, it seems that only pip3
is included for pip
$ ls -la /usr/bin/pip*
-rwxr-xr-x 1 root wheel 31488 3 18 00:42 /usr/bin/pip3
-- ~ / Library / Python
also has Python related files, but this contains the libraries installed with `` `pip```
--Werkzeug ・ A micro-framework for Python based on Jinja2, which is extremely lightweight. --Werkzeug: A simple WSGI utility library --WSGI (whiskey): I / F library for connecting a web server and a web application in Python --Jinja2: Template engine library often used in Python web development
--Only the core function of receiving the user's request and returning the result is implemented. --By default, it has only the above two libraries, so there are no extras such as DB and authentication.
--Scalable application creation is possible by combining extended functions (highly customizable)
--In Django, RDB is a set, but in Flask, NoSQL etc. can also be used
--High affinity with microservices
--Examples of services using Flask: Netflix, Lyft, LinkedIn, Mailgun, Pinterest
--A full stack framework that contains most of the functionality needed for web development
--Clean development according to the type is possible
--Examples of services using Django: Instagram, Bitbucket, Udemy, Dropbox, Spotify, Youtube
--Flask is an MTV framework (Model / Template / View) --Main processing flow --User accesses (request) a specific URL --Read the accessed URL and execute the process associated with the URL (View) --During the above process, access the DB through the Model object (Model) --At the end of the process, return a template such as HTM to be displayed to the user (Template) --The HTML returned to the browser is displayed
--When displaying templates in view (render_template), it is not necessary to include "/ templates" in the path --In Flask, it is a rule to create HTML files under the templates folder
--If you specify methods
in the routing settings, you can limit the HTTP methods allowed for that URL.
--If nothing is specified, only GET is allowed
--Since Jinja2 is used when rendering HTML with Flask, it is possible to use conditional branching in HTML and variables received from the view. --Example: ``` {% if not session.logged_in%}` ``
--Use `flash``` to display a message to the user --View: ``` flash ('message')`
--Template: `get_flashed_messages ()`
--If you use url_for
, the link name and method will be linked automatically, so you don't need to modify the code even if the link destination changes.
--Of course, it can be written not only on the view side but also on the template side code
--``` return render_template ('entries.html', entries = entries) `` `to pass arguments to the template
--```url_for ('method name', id = entry.id) `` `can be used to pass arguments to view methods
--app.route ('/ entries / <id>') You can receive the arguments passed by
url_forby doing something like
.
--If you want to limit the types of arguments you receive, use `<int: id>`
(other types will result in an error).
--Use redirect and render_template properly -Python --Is the part written as redirect useless in render? | teratail
--Model is defined as a class in Python
--Define the standard behavior when a Model is created with ``` def __init __ ()` ``
--``` def repr (self) `` `describes the output format of the console when the Model is referenced (it is not necessary)
--Once the Model is defined, it is necessary to reflect it in the DB (mainly the following two ways) --How to execute and reflect the Model definition on the console --How to create and reflect a script
--It is good to prepare a View for each Model and create a file by associating it one-to-one.
--Flask can handle session information with a variable called session
--Settings: session ['session name'] = True
--Confirmation: if not session.get ('session name'): (processing) `` ` --Destroy:
session.pop ('session name', None)`` --Second argument: Value to be returned if it has already been discarded (Specify ``
Noneunless otherwise specified) --Encryption: If you set
SECRET_KEY ='private key'` `` on the app itself, session information will be encrypted automatically
DB
--If you want to easily set up and use DB with Flask, use `` `Flask-SQLAlchemy``` library.
--Flask is an MTV framework, so if you define a Model, you can easily operate the DB using ORM.
--It is good to use a decorator for the processing in the View that appears repeatedly (extend with the library) --Example: Common session judgment as to whether or not you are logged in
Blueprint
--If you want to divide the functions of an app that has grown, use Blueprint (extend with a library). --When there are multiple functions in the app, the functions can be configured independently as a small app. ――For example, in the case of a blog, article-related functions and comment-related functions can be configured as independent apps. --View, Template, and static files for each application are made independent as a group (divisional structure) --Template and static files are common to each application, and it is possible to separate only View (functional structure) --When registering an application using Blueprint, it is also possible to add a prefix to the URL.
--@ app.errorhandler (status code at the time of error) `` `can define the process for each error status code. --When using Blueprint, use
@ (app name) .app_errorhandler (status code at the time of error)` ``
--flask command line can also be used
--The app can be started without using the py file for starting.
--You will be able to use libraries that are supposed to use the flask command (Flask-Migrate, etc.)
--Environment variables need to be set
-- $ export FLASK_APP = (path of directory with __init__.py) `` `: If you want to make it persistent, add it to the shell configuration file as appropriate. --
$ export FLASK_ENV = development```: When starting in debug mode (if not set, production mode)
--Application Factory allows you to create apps within functions instead of globally --The app created using this cannot be referenced directly from the outside, so call it using the `` `current_app``` method.
--There are various Python test libraries (this time I use Python standard unittest)
--Write the test DB settings in the application body file so that the test config can be read.
-- def setup (self) `` `: The first method to be executed when the test is executed --
def tearDown (self) `: Method executed just before the end of the test --``` def test _... ()
`: Test method body
--Test coverage can be measured with the `coverage``` library - ```$ pipenv install coverage``` --Create ``` .coveragerc``` and write the following to specify the folder to be tested. --``` [run] source = ./ (path)
`
- $ coverage run -m unittest
-- $ coverage report -m
: Coverage measurement
-- $ coverage html
: Creating a coverage report
--static
The directory and below are automatically recognized as places to put static files such as CSS and JS.
--Usage example: ``` <link rel =" stylesheet "href =" {{url_for ('static', filename ='style.css')}} ">
--There is a description to import `` `jquery-3.2.1.slim.min.js``` from the jQuery CDN, but at the moment it is necessary to rewrite 3.2.1 to 3.5.1.
Recommended Posts