Welcome to Python Flask tutorial. In previous lesson we have learnt about python lambda. Python flask is an API that helps us to build web based application in python. Let’s get started with python flask tutorial for beginners now.
欢迎使用Python Flask教程。 在上一课中,我们了解了python lambda 。 Python flask是一个API,可帮助我们在python中构建基于Web的应用程序。 让我们现在开始为初学者使用python flask教程。
First of all, we have to install python flask module. You can do it either by command prompt or by the help of IDE. I am using PyCharm Community Edition 2017.2. So I will install Flask using this IDE.
首先,我们必须安装python flask模块。 您可以通过命令提示符或IDE的帮助来完成此操作。 我正在使用PyCharm Community Edition 2017.2。 因此,我将使用此IDE安装Flask。
Open PyCharm then, click on file>settings. Then expand project and click on the Project Interpreter. You will see as following:
打开PyCharm,然后单击文件>设置 。 然后展开项目 ,然后单击“ 项目解释器” 。 您将看到以下内容:
I have already installed Flask, so the list is showing Flask. You have to click the + button on the right side of the window.
我已经安装了Flask,因此列表显示Flask。 您必须单击窗口右侧的+按钮。
Then Available package window will pop up. In the search bar type Flask, then it will appear.
然后将弹出可用包窗口。 在搜索栏中,键入Flask,然后它将出现。
Click on Flask and in the bottom of the window press install. Flask will be installed to your python if you are connected to the internet.
单击Flask,然后在窗口底部按install。 如果您已连接到互联网,则Flask将安装到您的python中。
You can install python flask module using pip
through below command.
您可以通过以下命令使用pip
安装python flask模块。
$pip install flask
So our installation of python flask module is done. Now we will start coding.
至此,我们完成了python flask模块的安装。 现在我们将开始编码。
Create a new project named FlaskTutorial. Then I am creating a python file named Main.py
. In this file we have the following code segment:
创建一个名为FlaskTutorial的新项目。 然后,我创建一个名为Main.py
的python文件。 在此文件中,我们具有以下代码段:
# importing flask module
from flask import Flask
# initializing a variable of Flask
app = Flask(__name__)
# decorating index function with the app.route
@app.route('/')
def index():
return "WELCOME!!! This is the home page"
if __name__ == "__main__":
app.run()
Then run the python file and you will see following in the console log:
然后运行python文件,您将在控制台日志中看到以下内容:
* Running on https://127.0.0.1:5000/ (Press CTRL+C to quit)
Now open your browser and copy paste the url (https://127.0.0.1:5000/
) that is shown in the console. Then you will get html response like below image.
现在打开浏览器并复制粘贴控制台中显示的URL( https://127.0.0.1:5000/
)。 然后您将得到如下图所示的html响应。
So this is our basic webpage served by python flask.
这是python flask提供的基本页面。
In the very first line we have imported Flask class of the flask module.
在第一行中,我们导入了flask模块的Flask类。
Then @app.route()
is the decorator that is decorating the index()
function.
然后@app.route()
是装饰index()
函数的装饰器 。
And we are setting the url of the index page as only ‘/’.
并且我们将索引页面的URL设置为仅“ /”。
The index() function returns a string which we see in the webpage. We can return html code also here. Finally we are checking whether the __main__
is directly running or not. If yes then running the app. Then we see the result in the browser.
index()函数返回我们在网页中看到的字符串。 我们也可以在这里返回html代码。 最后,我们正在检查__main__
是否直接运行。 如果是,则运行该应用程序。 然后,我们在浏览器中看到结果。
Now let’s look at another example where we will submit some data in the form and then display it in the next page.
现在让我们看另一个示例,我们将在表单中提交一些数据,然后在下一页中显示它们。
First create a templates folder. In this templates folder create two html file. As following:
首先创建一个模板文件夹。 在此模板文件夹中,创建两个html文件。 如下:
login.html
Now write the login.html
code as shown below.
login.html
现在,如下所示编写login.html
代码。
Home Page
Welcome to the Flask Tutorial
Please Register to Get the tutorials
The main functionality of this html file is to create a form having two input field and a submit button. Notice in the form tag we have written action="/FlaskTutorial"
. We will discuss about it later. Right now just remember it. All other is as usual for a html file.
该html文件的主要功能是创建一个具有两个输入字段和一个提交按钮的表单。 注意,在表单标签中,我们编写了action="/FlaskTutorial"
。 我们将在稍后讨论。 现在,只需记住它即可。 其他所有情况与html文件一样。
Now to see the output write in the Main.py
file. As we will use template of html, so in the first line we have imported render_template.
现在来看输出写在Main.py
文件中。 由于我们将使用html模板,因此在第一行中,我们导入了render_template。
# importing flask modules
from flask import Flask, render_template, request
# initializing a variable of Flask
app = Flask(__name__)
# decorating index function with the app.route with url as /login
@app.route('/login')
def index():
return render_template('login.html')
if __name__ == "__main__":
app.run()
In the browser paste this url ( https://127.0.0.1:5000/login
) and enter. You will see the output as below:
在浏览器中粘贴此URL( https://127.0.0.1:5000/login
)并输入。 您将看到如下输出:
Now we want to do what the submit button do. Submit button will send the email address to another Html file named success.html
that will show the email address with a success message.
现在,我们要做的是提交按钮。 提交按钮会将电子邮件地址发送到另一个名为success.html
HTML文件,该文件将显示带有成功消息的电子邮件地址。
Now let’s write the success.html code as shown below.
现在,让我们编写成功代码,如下所示。
success.html
success.html
Success
You have successfully registered with email {{ email }}
Notice that in the body tag there is a double curly brace. Inside this the email is the value that is sent from the login page. All other is as usual html code.
请注意,在body标签中有一个双花括号。 在此电子邮件中,是登录页面发送的值。 所有其他都是照常的html代码。
Now again back to the Main.py file, we have to write the following code.
现在再次回到Main.py文件,我们必须编写以下代码。
Main.py
Main.py
# importing flask module fro
from flask import Flask, render_template,request
# initializing a variable of Flask
app = Flask(__name__)
# decorating index function with the app.route with url as /login
@app.route('/login')
def index():
return render_template('login.html')
@app.route('/FlaskTutorial', methods=['POST'])
def success():
if request.method == 'POST':
email = request.form['email']
return render_template('success.html', email=email)
else:
pass
if __name__ == "__main__":
app.run()
Notice that we have added another route named /FlaskTutorial
. This one takes another argument named methods. In the return function we have sent an argument name email that we get from the text field of the login.html
using the action=/FlaskTutorial
and we have embed it in the success.html
file.
注意,我们添加了另一个名为/FlaskTutorial
路由。 这个采用另一个名为方法的参数。 在return函数中,我们发送了一个参数名电子邮件,该电子邮件是使用action=/FlaskTutorial
从login.html
的文本字段中获取的,并将其嵌入到success.html
文件中。
Now if we run the Main.py
, open browser with the url and give input as follows:
现在,如果我们运行Main.py
,则打开带有url的浏览器并输入如下:
Then pressing submit it will output:
然后按提交将输出:
So this is how you can send data from one html file to another html file using python flask. Download the complete code Here.
因此,这就是使用python flask将数据从一个html文件发送到另一个html文件的方法。 在此处下载完整的代码。
You can also send these data to your database using python mysql. To write python code in the html file you have to start with {% # some statements of python for inserting values in the database %}
. Start playing with python Flask.
您还可以使用python mysql将这些数据发送到数据库。 要在html文件中编写python代码,您必须从{% # some statements of python for inserting values in the database %}
开始, {% # some statements of python for inserting values in the database %}
。 开始玩python Flask。
That’s all for python flask tutorial for beginners.
这就是针对初学者的python flask教程的全部内容。
Reference: Github, Official Webpage.
参考: Github , 官方网页 。
翻译自: https://www.journaldev.com/15524/python-flask-tutorial