Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products. If you have feedback or questions as you navigate the site, click Send Feedback.

Python 2 is no longer supported by the community. We recommend that you migrate Python 2 apps to Python 3.

Serving Static Files

Stay organized with collections Save and categorize content based on your preferences.

This part of the Python Guestbook code walkthrough shows how to serve static files. App Engine does not serve files directly out of your application's source directory unless configured to do so. But there are many cases where you want to serve static files directly to the web browser. Images, CSS stylesheets, JavaScript code, movies, and Flash animations are all typically stored with a web application and served directly to the browser.

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, go to Creating a Guestbook.

Configuring the app to use static files

The CSS files for the Guestbook app are in the bootstrap/css directory. The template for the app's web page, index.html, instructs the browser to load bootstrap.css and bootstrap-responsive.css, which are static files:

<link type="text/css" rel="stylesheet" href="/bootstrap/css/bootstrap.css">
<link type="text/css" rel="stylesheet" href="/bootstrap/css/bootstrap-responsive.css">

The app.yaml file specifies the bootstrap directory as the location for static files:

- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /bootstrap
  static_dir: bootstrap

- url: /.*

The handlers section defines two handlers for URLs. When App Engine receives a request for a URL beginning with /bootstrap, it maps the remainder of the path to files in the bootstrap directory, and if an appropriate file is found, the contents of the file are returned to the client. All other URLs match the /.* pattern, and are handled by the app object in the guestbook module.

URL path patterns are tested in the order they appear in app.yaml. In this case, the /bootstrap pattern matches before the /.* pattern for the appropriate paths. For more information on URL mapping and other options you can specify in app.yaml, see the app.yaml reference.