DUSTIN INGRAM: I'm Dustin Ingram, developer advocate
for Google Cloud Platform.
I'm really excited to announce the general availability
of Python 3 Runtime on the App Engine standard environment.
Python is one of the most popular programming
languages and growing.
It's well suited for a number of use cases including
web applications, machine learning, and big data.
App Engine has supported Python Runtime
from the very beginning, more than 10 years now.
You may have tried Python on App Engine in the past
and noticed that it was very opinionated about what
you could and couldn't do.
This new Python Runtime is very different.
Python 3.7 on App Engine is our second generation Runtime.
That means it's based on open source Python
from the Python Software Foundation.
It supports Google Cloud client libraries
for services like Cloud storage, vision, translate, et cetera.
You can install libraries, frameworks,
and other dependencies using requirements.txt,
and best of all, Python 3.
I like to deploy web apps to App Engine
because my app scales automatically
up or down, even down to zero, to meet incoming traffic
spikes.
I only need to think about my Python code,
not the infrastructure or wiring that runs my app.
And I only pay for what my app uses,
and I can use App Engine's free tier to easily try new things.
In this video, I'll show you how to use
Flask, a popular and versatile web framework,
to quickly write, deploy, and test a Python 3 web
app on App Engine.
Once we've got the app deployed, we'll iterate on it a bit
to take advantage of some of those great new features.
There are three files that will make up our app.
app.yaml, main.py, and requirements.txt.
Let's start with app.yaml.
This file lets you configure your app
in a number of different ways.
All we need to do in this file is tell App Engine
that we want to use the new Python 3.7 Runtime.
We do this by adding these lines.
Next, moving down our list of files,
let's define our app's behavior by creating our main.py file.
This is where we can define the core logic of our application.
We'll call it main.py because that's
what App Engine uses by default. But you
can call it whatever you want as long as you configure it
appropriately.
We'll use Flask as our web framework,
but you can use any framework you'd like as well.
By default, App Engine looks for a variable called
app inside your main.py file.
As you might guess, this should correspond to your app.
We'll define an end point at the root URL that just says
hello, world, and that's it.
Finally, the new Python runtime understands
the same requirements format that
is used by PIP and other package installers and Runtimes
across the entire Python ecosystem,
the requirements.txt file.
Because we've chosen Flask as our web framework,
we'll specify it as a dependency.
In a file called requirements.txt,
we'll add Flask and pin its version
to be exactly version 1.0.2, the latest version that's currently
available.
Pinning the version of a dependency
ensures that the next time you deploy your app,
you won't install a newer and possibly incompatible
version of your dependency, which could lead to breakage.
So now we've defined these three files--
app.yaml, main.py, and requirements.txt.
Our app is finished.
Let's deploy it.
Deploying is easy with the gcloud command line tool.
Once you've installed it and authenticated yourself,
all you need to do is type gcloud app deploy
to deploy your app to App Engine.
Now that it's done, we can type gcloud app browse
to view the app.
Looks great.
Now let's see what else we can do.
Web apps are usually most useful when they can accept input as
well as produce output.
What if we just want to say hello to a specific person?
Let's see how we can do this with Flask.
By importing the request object, we
can inspect the query string parameters from the URL.
We'll look for one called name.
Let's deploy that real quick with gcloud app deploy.
If we don't pass any parameters, it stays the same
and says hi to the whole world.
But if we add a name=Python3 parameter to the end
of the URL, we've now passed that value into our app,
and it's rendered as you would expect.
Since this is Python 3, we can make our code
even better by using f strings instead of string concatenation
or by sticking a few emojis right into our source code.
Putting the emoji in line like that is great,
but what if we wanted the user to be
able to include an emoji of their own choice
by specifying it as text?
We could create a massive mapping from emoji names
to their actual values and find and replace them
in our name parameter.
Instead, let's use this third party emoji package,
which is open source and available on the Python Package
Index.
This package provides an emojize function,
which will let us turn text into emojis.
All we need to do is add the dependency
to our requirements.txt file like so.
Remember, always pin your dependencies.
Now, in main.py, we can import the emojize function
from the emoji library, and then use it
to emojize the entire greeting.
Once again, we'll deploy it with gcloud app deploy.
Now, let's add the string colon snake
colon to our name parameter and see what
it looks like in our browser.
Looks great.
So much more customizable.
So that's all there is to making a Python 3
app that handles user input and uses third party
libraries from PyPI.
Stay tuned for the next episode, where
I'll show you how to deploy a wag tail app to App Engine.
Feel free to ask questions in the comments below.
And if you like this video, please
subscribe to the GCP channel for more videos like this.
Thanks for watching.
[MUSIC PLAYING]