To install flash, first create a virtual environment that can install everything without affecting your main python. Another advantage is that you don’t need to have root privileges in this way.
Start building the environment: open a terminal window, select a location where you want to place the application, and create a new folder containing it. Let’s call the folder of this application microblog.
If you are using Python 3.4, go to the microblog directory first, and then create a virtual environment with the following command:
$ python -m venv flask
It should be noted that in some systems you may want to use Python 3 instead of Python. The above command line creates a complete Python environment in the flash folder.
If you are using Python versions below 3.4 (including Python 2.7), you need to download and install virtualenv.py before creating a virtual environment.
If you use Linux, you need to get a package. For example, if you use Ubuntu:
$ sudo apt-get install python-virtualenv
To create a virtual environment, enter the following command line $ virtualenv flask
Install flash and extensions one by one by entering the following command line:
$ flask/bin/pip install flask $ flask/bin/pip install flask-login $ flask/bin/pip install flask-openid $ flask/bin/pip install flask-mail $ flask/bin/pip install flask-sqlalchemy $ flask/bin/pip install sqlalchemy-migrate $ flask/bin/pip install flask-whooshalchemy $ flask/bin/pip install flask-wtf $ flask/bin/pip install flask-babel $ flask/bin/pip install guess_language $ flask/bin/pip install flipflop $ flask/bin/pip install coverage
I’m not going to show you one by one here.
The installation is now complete
Create the first flag project:
After the CD goes to the microblog folder, we start to create the basic file structure for the application: mkdir app mkdir app/static mkdir app/templates mkdir tmp
Let’s start with our app package (file APP)/__ init__. Py) to create a simple initialization script:
from flask import Flask app = Flask(__name__) from app import views
Let’s write the first view function (file app / views. Py)
from app import app @app.route('/') @app.route('/index') def index(): return "Hello, World!"
In fact, this view is very simple. It just returns a string to be displayed on the client’s web browser. Two route decorators create mappings from URL / and / index to this function.
The last step to a fully working web application is to create a script that starts our application development web server. Let’s call this script run. Py and put it in the root directory:
#!flask/bin/python from app import app app.run(debug = True)
This script simply imports the app variable from our app package and calls its run method to start the server. Remember that the app variable contains the flash instance we created earlier.
To start the application, you just need to run this script (run. Py). You have to make sure that this is an executable, and then you can run it:
chmod a+x run.py
The script can then be executed simply as follows:
If you do not perform the above operation, you can use it directly
After the server is initialized, it will listen to 5000 ports waiting to connect. Now open your web browser and enter the following URL:
You can also use this URL:
Finally, the author has a point to remind:
There may be an error in running run.py when some partners follow the manual
The reasons are as follows
The last line return does not backspace. Isn’t it embarrassing.
So there’s no problem:
Therefore, it is necessary to find a suitable IDE for coding in Ubuntu. Otherwise, it is easy to make mistakes just by opening a text editor. Of course, experts prefer to use a text editor to compile, so as to challenge their own QAQ.