I had the opportunity to build Python Flask from scratch, so I'll leave a note of how to build the environment.
Installation of Python using pyenv was done below, so if you haven't done it first, do the following first. Python environment construction mac windows
After completing the above, install virtualenv </ code>.
- What is Virtualenv? Virtualenv is a nice tool that allows you to build an individual environment for each project on Shell.
pip install virtualenv
If the above doesn't work, try running it with pip3 </ code>. Since I have an alias setting, it works with
pip </ code>.
alias pip=pip3
Create an environment after moving to an appropriate working directory.
The command is virtualenv {app_name} </ code>
cd /Work/flask_app/
$ virtualenv testapp
Using base prefix '/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7'
New python executable in /Work/flask_app/testapp/bin/python3.7
Also creating executable in /Users/yoshi/Work/sample_python/testapp/bin/python
Installing setuptools, pip, wheel...
done.
When completed, a directory (project?) Will be created with the specified {app_name} </ code> as shown below.
$ ll
total 0
drwxr-xr-x 6 ys staff 192 1 16 11:02 testapp/
Contents is like this
$ ll testapp/
total 0
drwxr-xr-x 6 ys staff 192 1 16 11:02 ./
drwxr-xr-x 4 ys staff 128 1 16 11:02 ../
lrwxr-xr-x 1 ys staff 80 1 16 11:02 .Python@ -> /usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/Python
drwxr-xr-x 18 ys staff 576 1 16 11:02 bin/
drwxr-xr-x 3 ys staff 96 1 16 11:02 include/
drwxr-xr-x 3 ys staff 96 1 16 11:02 lib/
You need to activate it to use this environment. Execute the following command on OS X and Linux.
$ cd testapp
$ . bin/activate
#If it doesn't work above
$ source bin/activate
(testapp) adminnoMacBook-Pro-4:testapp ys$
If it can be started successfully ( {app_name} </ code>), it will appear on the terminal and become executable.
$ pip install Flask
Collecting Flask
Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
|████████████████████████████████| 102kB 1.8MB/s
Collecting Werkzeug>=0.15
Downloading https://files.pythonhosted.org/packages/ce/42/3aeda98f96e85fd26180534d36570e4d18108d62ae36f87694b476b83d6f/Werkzeug-0.16.0-py2.py3-none-any.whl (327kB)
|████████████████████████████████| 327kB 1.9MB/s
Collecting itsdangerous>=0.24
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
|████████████████████████████████| 81kB 2.4MB/s
Collecting Jinja2>=2.10.1
Downloading https://files.pythonhosted.org/packages/65/e0/eb35e762802015cab1ccee04e8a277b03f1d8e53da3ec3106882ec42558b/Jinja2-2.10.3-py2.py3-none-any.whl (125kB)
|████████████████████████████████| 133kB 3.4MB/s
Collecting MarkupSafe>=0.23
Downloading https://files.pythonhosted.org/packages/ce/c6/f000f1af136ef74e4a95e33785921c73595c5390403f102e9b231b065b7a/MarkupSafe-1.1.1-cp37-cp37m-macosx_10_6_intel.whl
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, Flask
Successfully installed Flask-1.1.1 Jinja2-2.10.3 MarkupSafe-1.1.1 Werkzeug-0.16.0 click-7.0 itsdangerous-1.1.0
Check if it was completed successfully.
$ flask --version
Python 3.7.6
Flask 1.1.1
Werkzeug 0.16.0
Borrow a sample program from the official documentation.
quick start¶
The sample Python file can be directly under {app_name}.
sample_flask.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello Flask"
if __name__ == '__main__':
app.run()
python sample_flask.py
Will it work ...
moved!
$ python sample_flask.py
* Serving Flask app "test_flask" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [16/Jan/2020 11:33:03] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Jan/2020 11:33:03] "GET /favicon.ico HTTP/1.1" 404 -
The access to the TOP page is 200 </ code> properly. Even when I checked it with a browser, it was displayed properly.
However, for some reason, when I look at the console, I see * Environment: production </ code>. Since it is a development environment, the following warning is issued as it is not used for production as it is. (Currently there is no problem
WARNING: This is a development server. Do not use it in a production deployment.
If you are interested, execute the following command.
$ export FLASK_APP=app_name
$ export FLASK_ENV=development
(testapp) ys $ flask run
end.
Recommended Posts