Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask tutorial samples #391

Merged
merged 4 commits into from
Jun 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appengine/standard/flask/hello_world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
11 changes: 11 additions & 0 deletions appengine/standard/flask/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# App Engine Standard Flask Hello World

This sample shows how to use [Flask](http://flask.pocoo.org/) with Google App
Engine Standard.

Before running or deploying this application, install the dependencies using
[pip](http://pip.readthedocs.io/en/stable/):

pip install -t lib -r requirements.txt

For more information, see the [App Engine Standard README](../../README.md)
7 changes: 7 additions & 0 deletions appengine/standard/flask/hello_world/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
script: main.app
18 changes: 18 additions & 0 deletions appengine/standard/flask/hello_world/appengine_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.ext import vendor

# Add any libraries installed in the "lib" folder.
vendor.add('lib')
34 changes: 34 additions & 0 deletions appengine/standard/flask/hello_world/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app]
import logging

from flask import Flask


app = Flask(__name__)


@app.route('/')
def hello():
return 'Hello World!'


@app.errorhandler(500)
def server_error(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred.', 500
# [END app]
27 changes: 27 additions & 0 deletions appengine/standard/flask/hello_world/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest


@pytest.fixture
def app():
import main
main.app.testing = True
return main.app.test_client()


def test_index(app):
r = app.get('/')
assert r.status_code == 200
1 change: 1 addition & 0 deletions appengine/standard/flask/hello_world/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flask==0.11
1 change: 1 addition & 0 deletions appengine/standard/flask/tutorial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
11 changes: 11 additions & 0 deletions appengine/standard/flask/tutorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# App Engine Standard Flask Tutorial App

This sample shows how to use [Flask](http://flask.pocoo.org/) to handle
requests, forms, templates, and static files on Google App Engine Standard.

Before running or deploying this application, install the dependencies using
[pip](http://pip.readthedocs.io/en/stable/):

pip install -t lib -r requirements.txt

For more information, see the [App Engine Standard README](../../README.md)
11 changes: 11 additions & 0 deletions appengine/standard/flask/tutorial/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
runtime: python27
api_version: 1
threadsafe: true

# [START handlers]
handlers:
- url: /static
static_dir: static
- url: /.*
script: main.app
# [END handlers]
18 changes: 18 additions & 0 deletions appengine/standard/flask/tutorial/appengine_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.ext import vendor

# Add any libraries installed in the "lib" folder.
vendor.add('lib')
56 changes: 56 additions & 0 deletions appengine/standard/flask/tutorial/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app]
import logging

# [START imports]
from flask import Flask, render_template, request
# [END imports]

app = Flask(__name__)


# [START form]
@app.route('/form')
def form():
return render_template('form.html')
# [END form]


# [START submitted]
@app.route('/submitted', methods=['POST'])
def submitted_form():
name = request.form['name']
email = request.form['email']
site = request.form['site_url']
comments = request.form['comments']

# [END submitted]
# [START render_template]
return render_template(
'submitted_form.html',
name=name,
email=email,
site=site,
comments=comments)
# [END render_template]


@app.errorhandler(500)
def server_error(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred.', 500
# [END app]
38 changes: 38 additions & 0 deletions appengine/standard/flask/tutorial/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest


@pytest.fixture
def app():
import main
main.app.testing = True
return main.app.test_client()


def test_form(app):
r = app.get('/form')
assert r.status_code == 200
assert 'Submit a form' in r.data.decode('utf-8')


def test_submitted_form(app):
r = app.post('/submitted', data={
'name': 'Inigo Montoya',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You killed my father. prepare to die.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

'email': 'inigo@example.com',
'site_url': 'http://example.com',
'comments': ''})
assert r.status_code == 200
assert 'Inigo Montoya' in r.data.decode('utf-8')
1 change: 1 addition & 0 deletions appengine/standard/flask/tutorial/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flask==0.11
3 changes: 3 additions & 0 deletions appengine/standard/flask/tutorial/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.pagetitle {
color: #800080;
}
26 changes: 26 additions & 0 deletions appengine/standard/flask/tutorial/templates/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<html>
<head>
<title>Submit a form</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
</head>
<body>
<div id="container">
<div class="pagetitle">
<h1>Submit a form</h1>
</div>
<div id="main">
<form method="post" action="{{ url_for('submitted_form') }}">
<label for="name">Name:</label>
<input type="text" name="name"><br />
<label for="email">Email address:</label>
<input type="email" name="email"><br />
<label for="site_url">Website URL:</label>
<input type="url" name="site_url"><br />
<label for="comments">Comments:</label>
<textarea name="comments"></textarea><br />
<input type="submit">
</form>
</div>
</div>
</body>
</html>
23 changes: 23 additions & 0 deletions appengine/standard/flask/tutorial/templates/submitted_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<title>Submitted form</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
</head>
<body>
<div id="container">
<div class="pagetitle">
<h1>Form submitted</h1>
</div>
<div id="main">
<p>Thanks for your submission, {{name}}!</p>
<p>Here's a review of the information that you sent:</p>
<p>
<strong>Name</strong>: {{name}} <br>
<strong>Email</strong>: {{email}} <br>
<strong>Website URL</strong>: {{site}} <br>
<strong>Comments</strong>: {{comments}}
</p>
</div>
</div>
<body>
</html>
13 changes: 6 additions & 7 deletions nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,12 @@ def run_tests_in_sesssion(
if skip_flaky:
pytest_args.append('-m not slow and not flaky')

if sample_directories is None:
# session.posargs is any leftover arguments from the command line,
# which allows users to run a particular test instead of all of them.
if session.posargs:
sample_directories = session.posargs
else:
sample_directories = collect_sample_dirs('.', TESTS_BLACKLIST)
# session.posargs is any leftover arguments from the command line,
# which allows users to run a particular test instead of all of them.
if session.posargs:
sample_directories = session.posargs
elif sample_directories is None:
sample_directories = collect_sample_dirs('.', TESTS_BLACKLIST)

if changed_only:
changed_files = get_changed_files()
Expand Down