Tom Robinson
Tom Robinson

Reputation: 97

How do I make this flask app suitable for Google Cloud Engine?

I am currently trying to use google cloud engine to deploy my flask web app and I am not sure how I need to modify the folder structure and some of the file names to match the requirements.

I have a flask app structured like this:

.
├── app
│   ├── errors.py
│   ├── forms.py
│   ├── __init__.py
│   ├── models.py
│   ├── routes.py
│   ├── static
│   │   └── styles.css
│   └── templates
│       ├── 404.html
│       ├── 500.html
│       ├── archive.html
│       ├── base.html
│       ├── bootstrap.html
│       ├── editor.html
│       ├── index.html
│       ├── login.html
│       ├── _post.html
│       ├── post.html
│       └── tag.html
├── app.db
├── app.yaml
├── blog.py
├── config.py
└── requirements.txt

GCE's requirements look like the file structure on this page and include a main.py file at the top level.

I have blog.py at the top level which defines the application instance. Does this need to be the main.py file?

And would I need to modify the app.yaml file to represent this different file structure?

EDIT: I renamed blog.py to main.py and added the following lines to app/__init__.py:

if __name__ == '__main__':
     app.run(host='127.0.0.1', port=8080, debug=True)

But running python main.py does not make the app available on localhost.

Upvotes: 0

Views: 226

Answers (1)

sllopis
sllopis

Reputation: 2368

You are on the right track, the documentation should help you with the next steps, which you may or may not have already:

  1. Creating a templates/ folder.
  2. Creating a static/ folder.
  3. Writing the code in your main.py file.
  4. Specifying dependencies in the requirements.txt file.
  5. Definining your web service settings in the app.yaml file. You will need to modify the handler url to to point to the proper directories based on your project file structure. See the documentation for details.

To answer your question, this code:

if __name__ == '__main__':
  app.run(host='127.0.0.1', port=8080, debug=True)

is used when running locally only. When deploying to Google App Engine, a webserver process such as Gunicorn will serve the app. This can be configured by adding an entrypoint to app.yaml.

As the the documentationpoints out, "App Engine will also automatically add the gunicorn to your requirements.txt file if you don't specify the entrypoint field."

Finally, you will deploy your application to Google App Engine. You will issue the following command from the root directory of your project, where your app.yaml file is located:

gcloud app deploy

I found a nice image (credits to this article) that depicts an overview of the process:

enter image description here

Upvotes: 1

Related Questions