在Google App Engine上部署Python网络应用程序的快速指南

by Karan Asher

由Karan Asher

在Google App Engine上部署Python网络应用程序的快速指南 (A quick guide to deploying your Python webapp on Google App Engine)

The growth in the number of web-based applications and frameworks in the recent times is astounding. As companies such as Google, Amazon, and Microsoft provide more and more easy-to-use tools to build and deploy applications, it makes more sense to use the services and tools provided by them instead of building things in-house and hosting it on-premise.

近年来,基于Web的应用程序和框架的数量惊人地增长。 随着诸如Google,Amazon和Microsoft之类的公司提供越来越多的易于使用的工具来构建和部署应用程序,使用它们提供的服务和工具而不是内部构建并将其托管在其中更有意义。 -前提。

Google App Engine is a great way to get started with learning web development. It provides a bunch of useful features such as sharding, automatic database replication, automatic scaling, memcache, and so on.

Google App Engine是学习网络开发入门的好方法。 它提供了许多有用的功能,例如分片,自动数据库复制,自动缩放,内存缓存等。

However, the process to sign-up and deploy your first test hello world app is not very intuitive.

但是,注册和部署您的第一个测试Hello World应用程序的过程不是很直观。

In this, post, you will learn a very straightforward and easy to understand method to deploy your first Python webapp on Google App Engine. So let’s get started.

在本文中,您将学到一种非常简单易懂的方法, 可以在Google App Engine上部署您的第一个Python Webapp。 因此,让我们开始吧。

步骤1.下载基本的客房整理内容 (Step 1. Download the basic housekeeping stuff)

No matter what platform you build products on, there is always some housekeeping stuff you need to put in place before you can hit the ground running. And deploying apps within the Google App Engine is no exception.

不管您在什么平台上构建产品,总有一些整理工作需要摆放到位,然后才能开始运作。 在Google App Engine中部署应用程序也不例外。

  1. Download Python 2.7

    下载Python 2.7

    As of when this article was written, the Google App Engine

    撰写本文时,Google App Engine

    standard environment supports Python only upto version 2.7. However, it is only a matter of time before support for Python 3.x is added. You can check the App Engine docs for the latest info.

    标准环境仅支持python直到2.7版本 。 但是,添加对Python 3.x的支持只是时间问题。 您可以在App Engine文档中查看最新信息。

  2. Download Google Cloud SDK

    下载Google Cloud SDK

    This will allow you to fork apps onto your local machine, make changes (edit and develop the app), and deploy your app back to the cloud.

    这将允许您将应用程序分叉到本地计算机上,进行更改(编辑和开发应用程序),然后将应用程序部署回云中。

  3. Set the Python path in the Google App Engine launcher

    在Google App Engine启动器中设置Python路径

    After downloading the SDK, launch the App Engine launcher, go to Edit -> Preferences and make sure you set the path for where you installed Python in step 1 above.

    下载SDK之后,启动App Engine启动器,转到“编辑”->“偏好设置”,并确保在上面的步骤1中设置了安装Python的路径。

That’s all you need. Your local machine should now be ready to build webapps.

这就是您所需要的。 现在,您的本地计算机应已准备好构建Web应用程序。

第2步:App Engine注册 (Step 2. App Engine sign-up)

This is often the most confusing part of the entire setup. Things you should know when you sign-up:

这通常是整个设置中最令人困惑的部分。 注册时应了解的事项:

  1. Currently, App Engine offers a free trial for one year.

    目前,App Engine提供了一年的免费试用。
  2. The trial includes $300 of credit that can be used during the one year trial period.

    该试用版包括$ 300的信用额度,可在一年的试用期内使用。
  3. You will need to add a credit card to sign-up (for verification purposes).

    您将需要添加信用卡进行注册(出于验证目的)。
  4. You will not be charged during the sign-up process.

    在注册过程中不会向您收费。
  5. You will not be charged during the trial period as long as you do not cross the credit limit offered.

    只要您没有超过所提供的信用额度,就不会在试用期内向您收费。

Here are the steps you need to follow to sign-up:

以下是注册所需要执行的步骤:

  1. Go to the Google Cloud landing page

    转到Google Cloud登陆页面

  2. Follow the sign-up process and go to your App Engine dashboard

    按照注册过程进行操作,然后转到您的App Engine仪表板

Most of the hard work is complete after a successful sign-up.

成功注册后,大部分艰苦的工作就完成了。

步骤3.创建一个新项目 (Step 3. Create a new project)

The next step is to create a new Python project that you can work on. Follow the screenshots below to create a new project.

下一步是创建一个可以处理的新Python项目。 请按照下面的屏幕快照创建一个新项目。

Launch the new project wizard.

启动新的项目向导。

Give your app a name and make a note of your project ID.

给您的应用命名,并记下您的项目ID。

Hit the create button and Google should take a few minutes to set up all that is necessary for your newly created app.

点击“创建”按钮,Google应该花几分钟设置新创建的应用程序所需的所有内容。

步骤4.分叉应用程序以在本地进行开发 (Step 4. Fork the app to develop it locally)

The next step in the process is to fork the app on your local machine. This will allow you to make changes to the app locally and deploy it whenever you wish to.

该过程的下一步是将应用程序分叉到本地计算机上。 这样,您就可以在本地对应用程序进行更改,并在需要时进行部署。

Go to Google App Engine launcher and create a new application.

转到Google App Engine启动器并创建一个新的应用程序。

Enter the project ID of your newly created app. Also, provide the folder (local destination) where you wish to store the app locally. Make sure you select the Python 2.7 as your runtime engine.

输入新创建的应用程序的项目ID。 另外,提供您希望在本地存储应用程序的文件夹(本地目标)。 确保选择Python 2.7作为运行时引擎。

Hit the create button, and you should see your app listed on the window that follows. You should also check that you now see some files in your local storage (the directory you chose in the screenshot above) after this step.

点击创建按钮,您应该会在随后的窗口中看到您的应用程序。 在执行此步骤之后,您还应该检查一下是否可以在本地存储(在上面的屏幕快照中选择的目录)中看到一些文件。

步骤5.在本地运行应用程序 (Step 5. Run the app locally)

Before you go ahead and make some changes to the app, it is important to check whether or not you have executed all the above steps correctly. This can be done by simply running the app locally.

在继续对应用程序进行更改之前,请务必检查您是否正确执行了上述所有步骤,这一点很重要。 只需在本地运行应用程序即可完成此操作。

Select the app and hit the run button on the window.

选择应用程序,然后单击窗口上的运行按钮。

Wait for a few seconds until you can hit the Browse button. Once the Browse button becomes clickable, click it. This should take you to the browser, and you should see the hello world text appear in your browser window. Alternatively, you can manually go to the browser and use the port specified to access the app.

等待几秒钟,直到您可以单击“ 浏览”按钮。 一旦浏览按钮变为可单击,请单击它。 这将带您进入浏览器,并且您应该看到hello world文本出现在浏览器窗口中。 或者,您可以手动转到浏览器并使用指定的端口访问应用程序。

As long as you see the above screen, you are all set.

只要您看到上面的屏幕,便一切就绪。

步骤6.了解应用程序结构 (Step 6. Understand the app structure)

It is finally time to look at the lines of code which are running this webapp. Open your app folder in the text editor of your choice. I recommend Sublime text or VS Code. However, feel free to choose the one you prefer.

终于是时候来看看运行此Web应用程序的代码行。 在您选择的文本编辑器中打开您的应用程序文件夹。 我推荐Sublime文本VS Code 。 但是,请随意选择您喜欢的一种。

Here is a description of the various files.

这是各种文件的描述。

app.yaml

app.yaml

This file is a basic markup file that stores information (some metadata) about the app. It is important to note the following crucial parts of the file.

该文件是一个基本的标记文件,用于存储有关该应用程序的信息(一些元数据)。 重要的是要注意文件的以下关键部分。

  1. application

    应用

    This is the project ID which you should never change. This is the unique identifier for the app

    这是您永远不应更改的项目ID。 这是应用程序的唯一标识符

  2. url -> script

    url-> scr ipt

    This is the homepage for the app. In other words, this file will be rendered in your browser when you launch the app

    这是该应用程序的主页。 换句话说,启动应用程序时,该文件将在浏览器中呈现

  3. libraries

    图书馆

    This is where you can include external libraries to use within the webapp

    您可以在此处包含要在Webapp中使用的外部库

main.py

main.py

This is the homepage of the app (as discussed above). Note that the hello world text in the browser window (step 5) is due to the code you see highlighted below.

这是应用程序的主页(如上所述)。 请注意,浏览器窗口中的hello world文本(第5步)是由于您在下面看到的突出显示的代码。

步骤7.进行更改并部署新应用 (Step 7. Make your changes and deploy the new app)

No hello world app is ever complete without the developer changing the hello world text to something else just to make sure that everything happening behind the scenes is working as it should.

没有开发人员将hello world文本更改为其他内容,只是为了确保幕后发生的所有事情都能正常工作,hello world应用程序是永远不会完成的。

Go ahead and change the text in the above screenshot to something else.

继续并将上述屏幕截图中的文本更改为其他内容。

Save the changes, go to the browser and refresh the page. You should see the page with the text “MEOW” displayed.

保存更改,进入浏览器并刷新页面。 您应该看到显示文本“ MEOW”的页面。

Finally, it is time to deploy your changes to the cloud to make them globally accessible via a URL. Go to the App Engine launcher, select the app, and hit the Deploy button.

最后,是时候将您的更改部署到云中,以使其可以通过URL在全球范围内访问了。 转到App Engine启动器,选择应用程序,然后点击Deploy按钮。

This will ensure your app gets deployed onto Google Cloud. To check whether or not everything worked just fine, go to the URL below:

这将确保您的应用程序已部署到Google Cloud上。 要检查一切是否正常,请转到以下网址:

https://<yourProjectID>.appspot.com/

https:// < yourProjectID > .appspo t.com/

You should see the exact same window as above, expect now, it is a URL that is globally accessible.

您应该看到与上面完全相同的窗口,现在应该可以看到,它是可以全局访问的URL。

步骤8.其他 (Step 8. Misc)

Congratulations, you’ve finally gotten your first Python webapp deployed on the Google App Engine. Here are some other points which you may find useful.

恭喜,您终于在Google App Engine上部署了第一个Python网络应用程序。 以下是一些其他有用的地方。

  1. Jinja 2 is an amazing front end templating library for Python that can do some cool stuff, such as passing objects form Python to HTML, using for loops, if conditions, and so on directly out of the box

    Jinja 2是一个很棒的Python前端模板库,可以完成一些很酷的工作,例如将对象从Python传递到HTML,使用for循环,条件等直接使用。

  2. Here’s a very useful Udacity course on web development that I have personally found quite resourceful

    这是一本关于Web开发的非常有用的Udacity课程,我个人认为非常有用

  3. Viewing the logs while running your webapp can be handy to debug and also discover some bugs on the fly

    在运行Web应用程序时查看日志可以方便地调试,并且还可以即时发现一些错误

#UntilNextTime.

#UntilNextTime

翻译自: https://www.freecodecamp.org/news/how-to-deploy-your-first-python-webapp-on-google-app-engine-2d487b52796a/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值