30
loading...
This website collects cookies to deliver better user experience
python3
, or if that does not work, just python
. Here is what you should expect to see:$ python
Python 3.9.4 (tags/v3.9.4:1f2e308, Apr 4 2021, 13:27:16) [MSC v.1928 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
exit()
and press Enter. On the Linux and Mac OS X versions of Python you can also exit the interpreter by pressing Ctrl-D. On Windows, the exit shortcut is Ctrl-Z followed by Enter.pip
that does this work (in Python 2.7 pip
does not come bundled with Python and needs to be installed separately).pip
as follows:$ pip install <package-name>
pip
tool is going to download the package from PyPI, and then add it to your Python installation. From that point on, every Python script that you have on your system will have access to this package. Imagine a situation where you have completed a web application using version 0.11 of Flask, which was the most current version of Flask when you started, but now has been superseeded by version 0.12. You now want to start a second application, for which you'd like to use the 0.12 version, but if you replace the 0.11 version that you have installed you risk breaking your older application. Do you see the problem? It would be ideal if it was possible to install Flask 0.11 to be used by your old application, and also install Flask 0.12 for your new one.$ mkdir miniblog
$ cd miniblog
$ python3 -m venv venv
venv
package, which creates a virtual environment named venv
. The first venv
in the command is the name of the Python virtual environment package, and the second is the virtual environment name that I'm going to use for this particular environment. If you find this confusing, you can replace the second venv
with a different name that you want to assign to your virtual environment. In general I create my virtual environments with the name venv
in the project directory, so whenever I cd
into a project I find its corresponding virtual environment.python
instead of python3
in the command above. Some installations use python
for Python 2.x releases and python3
for the 3.x releases, while others map python
to the 3.x releases.$ virtualenv venv
$ source venv/bin/activate
(venv) $ _
$ venv\Scripts\activate
(venv) $ _
python
. Also, the terminal prompt is modified to include the name of the activated virtual environment. The changes made to your terminal session are all temporary and private to that session, so they will not persist when you close the terminal window. If you work with multiple terminal windows open at the same time, it is perfectly fine to have different virtual environments activated on each one.(venv) $ pip install flask
>>> import flask
>>> _
app
, that will host the application. Make sure you are in the microblog directory and then run the following command:(venv) $ mkdir app
app
package is going to contain the following code:from flask import Flask
app = Flask(__name__)
from app import routes
Flask
imported from the flask package. The __name__
variable passed to the Flask
class is a Python predefined variable, which is set to the name of the module in which it is used. Flask uses the location of the module passed here as a starting point when it needs to load associated resources such as template files, which I will cover in Chapter 2. For all practical purposes, passing __name__
is almost always going to configure Flask in the correct way. The application then imports the routes
module, which doesn't exist yet.app
. The app
package is defined by the app directory and the __init__.py script, and is referenced in the from app import routes
statement. The app
variable is defined as an instance of class Flask
in the __init__.py script, which makes it a member of the app
package.routes
module is imported at the bottom and not at the top of the script as it is always done. The bottom import is a workaround to circular imports, a common problem with Flask applications. You are going to see that the routes
module needs to import the app
variable defined in this script, so putting one of the reciprocal imports at the bottom avoids the error that results from the mutual references between these two files.routes
module? The routes are the different URLs that the application implements. In Flask, handlers for the application routes are written as Python functions, called view functions. View functions are mapped to one or more route URLs so that Flask knows what logic to execute when a client requests a given URL.from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
@app.route
lines above the function are decorators, a unique feature of the Python language. A decorator modifies the function that follows it. A common pattern with decorators is to use them to register functions as callbacks for certain events. In this case, the @app.route
decorator creates an association between the URL given as an argument and the function. In this example there are two decorators, which associate the URLs /
and /index
to this function. This means that when a web browser requests either of these two URLs, Flask is going to invoke this function and pass the return value of it back to the browser as a response. If this does not make complete sense yet, it will in a little bit when you run this application.from app import app
app
entities? Here you can see both together in the same sentence. 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. If you find this confusing, you can rename either the package or the variable to something else.miniblog/
venv/
app/
__init__.py
routes.py
miniblog.py
FLASK_APP
environment variable:(venv) $ export FLASK_APP=miniblog.py
set
instead of export
in the command above.(venv) $ flask run
* Serving Flask app "miniblog"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
flask run
indicates that the server is running on IP address 127.0.0.1, which is always the address of your own computer. This address is so common that is also has a simpler name that you may have seen before: localhost. Network servers listen for connections on a specific port number. Applications deployed on production web servers typically listen on port 443, or sometimes 80 if they do not implement encryption, but access to these ports require administration rights. Since this application is running in a development environment, Flask uses the freely available port 5000. Now open up your web browser and enter the following URL in the address field: http://localhost:5000/
http://localhost:5000/index
/
, while the second maps to /index
. Both routes are associated with the only view function in the application, so they produce the same output, which is the string that the function returns. If you enter any other URL you will get an error, since only these two URLs are recognized by the application.FLASK_APP
environment variable when you open a new terminal window. Starting with version 1.0, Flask allows you to register environment variables that you want to be automatically imported when you run the flask
command. To use this option you have to install the python-dotenv package:(venv) $ pip install python-dotenv
FLASK_APP=miniblog.py