flask程序打包部署_如何使用Flask构建Web应用程序并将其部署到云中

本文介绍了如何使用Python的Flask框架构建Web应用程序,并将其部署到云端。首先,讲解了HTTP协议与Flask的关系,然后详细阐述了Flask的工作原理和如何编写基本的Flask应用。接着,讨论了HTML、CSS在Flask中的应用,并使用virtualenv管理项目环境。最后,展示了如何将应用部署到Google App Engine上,包括配置和部署步骤。
摘要由CSDN通过智能技术生成

flask程序打包部署

by Salvador Villalon

萨尔瓦多·维拉隆(Salvador Villalon)

介绍 (Introduction)

In each section, I will show pieces of code for you to follow along. All the code used in the tutorial is available in this GitHub Repository.

在每个部分中,我将显示一些代码供您遵循。 本教程中使用的所有代码都可以在GitHub Repository中找到

什么是HTTP,它与Flask有什么关系? (What is HTTP and What Does it Have to do with Flask?)

HTTP is the protocol for websites. The internet uses it to interact and communicate with computers and servers. Let me give you an example of how you use it everyday.

HTTP是网站的协议。 互联网使用它与计算机和服务器进行交互和通信。 让我举一个例子,说明您每天如何使用它。

When you type the name of a website in the address bar of your browser and you hit enter. What happens is that an HTTP request has been sent to a server.

在浏览器的地址栏中键入网站名称时,按Enter键。 发生的情况是HTTP请求已发送到服务器。

For example, when I go to my address bar and type google.com, then hit enter, an HTTP request is sent to a Google Server. The Google Server receives the request and needs to figure how to interpret that request. The Google Server sends back an HTTP response that contains the information that my web browser receives. Then it displays what you asked for on a page in the browser.

例如,当我转到地址栏并键入google.com,然后按Enter键时,HTTP请求将发送到Google服务器。 Google服务器接收到该请求,并需要弄清楚如何解释该请求。 Google服务器发回一个HTTP响应,其中包含我的网络浏览器收到的信息。 然后,它在浏览器的页面上显示您要的内容。

Flask如何参与? (How is Flask involved?)

We will write code that will take care of the server side processing. Our code will receive requests. It will figure out what those requests are dealing with and what they are asking. It will also figure out what response to send to the user.

我们将编写代码来处理服务器端的处理。 我们的代码将收到请求。 它将弄清楚那些请求正在处理什么以及它们在问什么。 它还将找出发送给用户的响应。

To do all this we will use Flask.

为此,我们将使用Flask。

什么是烧瓶? (What is Flask?)

It makes the process of designing a web application simpler. Flask lets us focus on what the users are requesting and what sort of response to give back.

它使设计Web应用程序的过程更加简单。 烧瓶让我们专注 关于用户的要求以及回馈的方式。

Learn more about micro frameworks.

了解有关微框架的更多信息。

Flask应用程序如何工作? (How Does a Flask App Work?)

The code lets us run a basic web application that we can serve, as if it were a website.

该代码使我们可以运行一个可以服务的基本Web应用程序,就像它是一个网站一样。

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World!"
    
if __name__ == "__main__":
    app.run(debug=True)

This piece of code is stored in our main.py.

这段代码存储在我们的main.py中。

Line 1: Here we are importing the Flask module and creating a Flask web server from the Flask module.

第1行:在这里,我们导入Flask模块,并从Flask模块创建Flask Web服务器。

Line 3: __name__ means this current file. In this case, it will be main.py. This current file will represent my web application.

第3行:__name__表示当前文件 。 在这种情况下,它将是main.py。 当前文件将代表我的Web应用程序。

We are creating an instance of the Flask class and calling it app. Here we are creating a new web application.

我们正在创建Flask类的实例,并将其称为app。 在这里,我们正在创建一个新的Web应用程序。

Line 5: It represents the default page. For example, if I go to a website such as “google.com/” with nothing after the slash. Then this will be the default page of Google.

第5行:它代表默认页面。 例如,如果我转到“ google.com/”之类的网站后,斜杠后一无所有。 然后,这将是Google的默认页面。

Line 6–7: When the user goes to my website and they go to the default page (nothing after the slash), then the function below will get activated.

第6–7行 :当用户访问我的网站并转到默认页面(斜杠后没有其他字符)时,下面的功能将被激活。

Line 9: When you run your Python script, Python assigns the name “__main__” to the script when executed.

第9行:运行Python脚本时,Python在执行时会为脚本分配名称“ __main__”。

If we import another script, the if statement will prevent other scripts from running. When we run main.py, it will change its name to __main__ and only then will that if statement activate.

如果我们导入另一个脚本,则if语句将阻止其他脚本运行。 当我们运行main.py时,它将名称更改为__main__,然后if语句激活。

Line 10: This will run the application. Having debug=True allows possible Python errors to appear on the web page. This will help us trace the errors.

第10行:这将运行应用程序。 使用debug=True可使可能的Python错误显示在网页上。 这将帮助我们跟踪错误。

让我们尝试运行main.py (Let’s Try Running main.py)

In your Terminal or Command Prompt go to the folder that contains your main.py. Then do py main.py or python main.py. In your terminal or command prompt you should see this output.

在终端或命令提示符下,转到包含main.py的文件夹。 然后做py main.pypython main.py 在终端或命令提示符下,您应该看到此输出。

The important part is where it says Running on http://127.0.0.1:5000/.

重要的部分是它说Running on http://127.0.0.1:5000/

127.0.0.1 means this local computer. If you do not know the meaning of this (like I didn’t when I started — this article is really helpful), the main idea is that 127.0.0.1 and localhost refer to this local computer.

127.0.0.1表示此本地计算机。 如果您不知道它的含义(例如我刚开始时并不了解— 这篇文章确实有帮助 ),则主要思想是127.0.0.1和localhost引用此本地计算机。

Go to that address and you should see the following:

转到该地址,您应该看到以下内容:

Flask带来更多乐趣 (More Fun with Flask)

Earlier you saw what happened when we ran main.py with one route which was app.route(“/”).

之前,您看到了当我们使用一个名为app.route(“ /”)的路由运行main.py时发生的情况。

Let’s add more routes so you can see the difference.

让我们添加更多路线,以便您可以看到不同之处。

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World!"
    
@app.route("/salvador")
def salvador():
    return "Hello, Salvador"
    
if __name__ == "__main__":
    app.run(debug=True)

In lines 9–11. we added a new route, this time to /salvador.

9-11行中 。 我们这次添加了一条新路线到/ salvador。

Now run the main.py again and go to http://localhost:5000/salvador.

现在再次运行main.py并转到 http:// localhost:5000 / salvador

So far we have been returning text. Let’s make our website look nicer by adding HTML and CSS.

到目前为止,我们一直在返回文本。 通过添加HTML和CSS,使我们的网站看起来更好。

HTML࿰

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值