欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

The Flask Mega-Tutorial 之 Chapter 1: Hello, World!

程序员文章站 2024-03-23 11:49:22
...

To learn Flask,I start to follow MiguelGrinberg’s 《The Flask Mega-Tutorial》 .

Install Python

  • python –version = 3.6.4 (different from tutorial’s 3.5.2)

Install Flask

  • Flask –version = 1.0.2

Project Root

  • ~ / Flask_microblog

Virtual Environment

  • pipenv –python 3.6.4 (executed under Flask_microblog/)

    • tutorial’s: $ python3 -m venv venv
    • virtualenv venv (Python older than 3.4, including 2.7 )

pipenv

  • pipenv usage
$ pipenv
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --where          Output project home information.
  --venv           Output virtualenv information.
  --py             Output Python interpreter information.
  --envs           Output Environment Variable options.
  --rm             Remove the virtualenv.
  --bare           Minimal output.
  --completion     Output completion (to be eval'd).
  --man            Display manpage.
  --three / --two  Use Python 3/2 when creating virtualenv.
  --python TEXT    Specify which version of Python virtualenv should use.
  --site-packages  Enable site-packages for the virtualenv.
  --version        Show the version and exit.
  -h, --help       Show this message and exit.


Usage Examples:
   Create a new project using Python 3.6, specifically:
   $ pipenv --python 3.6

   Install all dependencies for a project (including dev):
   $ pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

Commands:
  check      Checks for security vulnerabilities and against PEP 508 markers
             provided in Pipfile.
  clean      Uninstalls all packages not specified in Pipfile.lock.
  graph      Displays currently–installed dependency graph information.
  install    Installs provided packages and adds them to Pipfile, or (if none
             is given), installs all packages.
  lock       Generates Pipfile.lock.
  open       View a given module in your editor.
  run        Spawns a command installed into the virtualenv.
  shell      Spawns a shell within the virtualenv.
  sync       Installs all packages specified in Pipfile.lock.
  uninstall  Un-installs a provided package and removes it from Pipfile.
  • Locate the project:
$ pipenv --where
/Users/kennethreitz/Library/Mobile Documents/com~apple~CloudDocs/repos/kr/pipenv/test
  • Locate the virtualenv:
$ pipenv --venv
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre
  • Locate the Python interpreter:
$ pipenv --py
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre/bin/python
  • Install packages:
$ pipenv install
Creating a virtualenv for this project...
...
No package provided, installing all dependencies.
Virtualenv location: /Users/kennethreitz/.local/share/virtualenvs/test-EJkjoYts
Installing dependencies from Pipfile.lock...
...

To activate this project's virtualenv, run the following:
$ pipenv shell
  • Install a dev dependency:
$ pipenv install pytest --dev
Installing pytest...
...
Adding pytest to Pipfile's [dev-packages]...
  • Show a dependency graph:
$ pipenv graph
requests==2.18.4
  - certifi [required: >=2017.4.17, installed: 2017.7.27.1]
  - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
  - idna [required: >=2.5,<2.7, installed: 2.6]
  - urllib3 [required: <1.23,>=1.21.1, installed: 1.22]
  • Generate a lockfile:
$ pipenv lock
Assuring all dependencies from Pipfile are installed...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Note: your project now has only default [packages] installed.
To install [dev-packages], run: $ pipenv install --dev
  • Install all dev dependencies:
$ pipenv install --dev
Pipfile found at /Users/kennethreitz/repos/kr/pip2/test/Pipfile. Considering this to be the project home.
Pipfile.lock out of date, updating...
Assuring all dependencies from Pipfile are installed...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
  • Uninstall everything:
$ pipenv uninstall --all
No package provided, un-installing all dependencies.
Found 25 installed package(s), purging...
...
Environment now purged and fresh!
  • Use the shell:
$ pipenv shell
Loading .env environment variables…
Launching subshell in virtual environment. Type 'exit' or 'Ctrl+D' to return.
$ 

Flask application instance

  • Flask_microblog / app / __init__.py
from flask import Flask

app = Flask(__name__)

from app import routes

Note:

  • when a __init__.py is added into a directory, the directory would become a python package, thus can be imported by other module.
  • Thus, the app folder become a package, and the inside instance ‘app’ can be imported by:
    from app import app

routes.py

  • Flask_microblog / app / routes.py: Home page route
from app import app

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"

Main application module

from app import app
  • The Flask application instance is called app and is a member of the app package. The from app import app statement imports the app variable that is a member of the app package.

Structure (initial)

Flask_microblog
├── app
│   ├── __init__.py
│   ├── routes.py
│   └── templates
├── microblog.py
├── Pipfile
└── Pipfile.lock

flask run

(venv) $ export FLASK_APP=microblog.py

Execute this command under the top-level directory, where microblog.py is placed, i.e. Flask_microblog / microblog.py .