by Karan Asher
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.
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.
However, the process to sign-up and deploy your first test hello world app is not very intuitive.
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.
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.
- Download Python 2.7
As of when this article was written, the 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.
- 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
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.
That’s all you need. Your local machine should now be ready to build webapps.
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:
- Currently, App Engine offers a free trial for one year.
- The trial includes $300 of credit that can be used during the one year trial period.
- You will need to add a credit card to sign-up (for verification purposes).
- You will not be charged during the sign-up process.
- 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:
- Go to the Google Cloud landing page
- Follow the sign-up process and go to your App Engine dashboard
Most of the hard work is complete after a successful sign-up.
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.
Launch the new project wizard.
Give your app a name and make a note of your project ID.
Hit the create button and Google should take a few minutes to set up all that is necessary for your newly created app.
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.
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.
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.
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.
As long as you see the above screen, you are all set.
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.
Here is a description of the various files.
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.
This is the project ID which you should never change. This is the unique identifier for the app
- url -> script
This is the homepage for the app. In other words, this file will be rendered in your browser when you launch the app
This is where you can include external libraries to use within the webapp
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.
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.
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.
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.
This will ensure your app gets deployed onto Google Cloud. To check whether or not everything worked just fine, go to the URL below:
You should see the exact same window as above, expect now, it is a URL that is globally accessible.
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.
- 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
- Here’s a very useful Udacity course on web development that I have personally found quite resourceful
- Viewing the logs while running your webapp can be handy to debug and also discover some bugs on the fly