Flask is a lightweight web development framework based on Python, this article outlines its basic steps for building web use, the following default development environment is Ubuntu14.04.
This article refers to the Flask official building tutorial translation and collation: http://flask.pocoo.org/docs/0.10/
At the same time, based on the flask official tutorials provided by the Example_code to explain:
One. Installation
1. Install Python
~$: sudo apt-get install python
2. Installing irtualenv
Virtualenv can develop a virtual environment for Python so that when you have a lot of development projects and rely on different versions of Python, you can develop them in a single virtual environment, without impacting each other.
~$: sudo apt-get install python-virtualenv
Two. Create a virtual environment
1. Select a working directory
We set up "Tmp/flask" in our home directory as the working directory for this instance
~$:p WD
~$: ~/tmp/flask/$
2. Create a virtual environment
We create a virtual environment for this example project under the ~/flask directory:
~/tmp/flask$: virtualenv venv
After executing the command you can see a venv directory in the Flask directory, which is the virtual environment created
3. Activating the virtual environment
Activate the virtual environment with the following command:
~/tmp/flask$:. Venv/bin/activate
If the activation succeeds, you will see the command prompt at the beginning with "(venv)", as follows:
(venv) [Email protected]:~/tmp/flask$
This allows us to freely configure this virtual environment without worrying about destroying the real system environment or conflicting with the environment configuration of other projects.
4. Installing flask in a virtual environment
When the virtual environment is activated, we execute commands in the current command-line window to install some tools, of course, these installations are only valid for this virtual environment
We use the PIP command to install the Python package (the default with PIP when creating the virtual environment), we first install the Flask package:
env) [Email protected]~/tmp/flask$: Pip Install flask
The environment is configured to complete
Three. HelloWorld
OK, so hurry up and write a helloworld example
Edit a helloworld.py file with the following content:
From flask import Flaskapp = Flask (__name__) @app. Route ('/') def RootDir (): return "Hello, world!" if __name__ = = ' __main__ ': app.run (host= ' 0.0.0.0 ')
Then execute the following command at the command line to start the Web app:
(venv) [email protected]:~/tmp/flask$: Python helloworld.py
Then visit "Http://0.0.0.0:5000/" in the browser to return to helloworld!
Flask Getting Started 1-helloworld