by Karan Asher
由Karan Asher
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。 因此,让我们开始吧。
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中部署应用程序也不例外。
Download 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文档中查看最新信息。
Download 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.
这将允许您将应用程序分叉到本地计算机上,进行更改(编辑和开发应用程序),然后将应用程序部署回云中。
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应用程序。
This is often the most confusing part of the entire setup. Things you should know when you sign-up:
这通常是整个设置中最令人困惑的部分。 注册时应了解的事项:
Here are the steps you need to follow to sign-up:
以下是注册所需要执行的步骤:
Go to the Google Cloud landing page
转到Google Cloud登陆页面
Most of the hard work is complete after a successful sign-up.
成功注册后,大部分艰苦的工作就完成了。
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应该花几分钟设置新创建的应用程序所需的所有内容。
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.
点击创建按钮,您应该会在随后的窗口中看到您的应用程序。 在执行此步骤之后,您还应该检查一下是否可以在本地存储(在上面的屏幕快照中选择的目录)中看到一些文件。
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.
只要您看到上面的屏幕,便一切就绪。
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.
该文件是一个基本的标记文件,用于存储有关该应用程序的信息(一些元数据)。 重要的是要注意文件的以下关键部分。
application
应用
This is the project ID which you should never change. This is the unique identifier for the app
这是您永远不应更改的项目ID。 这是应用程序的唯一标识符
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
这是该应用程序的主页。 换句话说,启动应用程序时,该文件将在浏览器中呈现
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步)是由于您在下面看到的突出显示的代码。
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。
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网络应用程序。 以下是一些其他有用的地方。
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循环,条件等直接使用。
Here’s a very useful Udacity course on web development that I have personally found quite resourceful
#UntilNextTime.
#UntilNextTime 。