10000 Flask tutorial samples (#391) · kler/python-docs-samples@56466d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 56466d1

Browse files
author
Jon Wayne Parrott
authored
Flask tutorial samples (GoogleCloudPlatform#391)
1 parent 3fd9905 commit 56466d1

File tree

18 files changed

+293
-7
lines changed

18 files changed

+293
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# App Engine Standard Flask Hello World
2+
3+
This sample shows how to use [Flask](http://flask.pocoo.org/) with Google App
4+
Engine Standard.
5+
6+
Before running or deploying this application, install the dependencies using
7+
[pip](http://pip.readthedocs.io/en/stable/):
8+
9+
pip install -t lib -r requirements.txt
10+
11+
For more information, see the [App Engine Standard README](../../README.md)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: true
4+
5+
handlers:
6+
- url: /.*
7+
script: main.app
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.ext import vendor
16+
17+
# Add any libraries installed in the "lib" folder.
18+
vendor.add('lib')
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START app]
16+
import logging
17+
18+
from flask import Flask
19+
20+
21+
app = Flask(__name__)
22+
23+
24+
@app.route('/')
25+
def hello():
26+
return 'Hello World!'
27+
28+
29+
@app.errorhandler(500)
30+
def server_error(e):
31+
# Log the error and stacktrace.
32+
logging.exception('An error occurred during a request.')
33+
return 'An internal error occurred.', 500
34+
# [END app]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
18+
@pytest.fixture
19+
def app():
20+
import main
21+
main.app.testing = True
22+
return main.app.test_client()
23+
24+
25+
def test_index(app):
26+
r = app.get('/')
27+
assert r.status_code == 200
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask==0.11
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# App Engine Standard Flask Tutorial App
2+
3+
This sample shows how to use [Flask](http://flask.pocoo.org/) to handle
4+
requests, forms, templates, and static files on Google App Engine Standard.
5+
6+
Before running or deploying this application, install the dependencies using
7+
[pip](http://pip.readthedocs.io/en/stable/):
8+
9+
pip install -t lib -r requirements.txt
10+
11+
For more information, see the [App Engine Standard README](../../README.md)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: true
4+
5+
# [START handlers]
6+
handlers:
7+
- url: /static
8+
static_dir: static
9+
- url: /.*
10+
script: main.app
11+
# [END handlers]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.ext import vendor
16+
17+
# Add any libraries installed in the "lib" folder.
18+
vendor.add('lib')
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START app]
16+
import logging
17+
18+
# [START imports]
19+
from flask import Flask, render_template, request
20+
# [END imports]
21+
22+
app = Flask(__name__)
23+
24+
25+
# [START form]
26+
@app.route('/form')
27+
def form():
28+
return render_template('form.html')
29+
# [END form]
30+
31+
32+
# [START submitted]
33+
@app.route('/submitted', methods=['POST'])
34+
def submitted_form():
35+
name = request.form['name']
36+
email = request.form['email']
37+
site = request.form['site_url']
38+
comments = request.form['comments']
39+
40+
# [END submitted]
41+
# [START render_template]
42+
return render_template(
43+
'submitted_form.html',
44+
name=name,
45+
email=email,
46+
site=site,
47+
comments=comments)
48+
# [END render_template]
49+
50+
51+
@app.errorhandler(500)
52+
def server_error(e):
53+
# Log the error and stacktrace.
54+
logging.exception('An error occurred during a request.')
55+
return 'An internal error occurred.', 500
56+
# [END app]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
18+
@pytest.fixture
19+
def app():
20+
import main
21+
main.app.testing = True
22+
return main.app.test_client()
23+
24+
25+
def test_form(app):
26+
r = app.get('/form')
27+
assert r.status_code == 200
28+
assert 'Submit a form' in r.data.decode('utf-8')
29+
30+
31+
def test_submitted_form(app):
32+
r = app.post('/submitted', data={
33+
'name': 'Inigo Montoya',
34+
'email': 'inigo@example.com',
35+
'site_url': 'http://example.com',
36+
'comments': ''})
37+
assert r.status_code == 200
38+
assert 'Inigo Montoya' in r.data.decode('utf-8')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask==0.11
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.pagetitle {
2+
color: #800080;
3+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
<head>
3+
<title>Submit a form</title>
4+
<link rel="stylesheet" type="text/css" href="/static/style.css">
5+
</head>
6+
<body>
7+
<div id="container">
8+
<div class="pagetitle">
9+
<h1>Submit a form</h1>
10+
</div>
11+
<div id="main">
12+
<form method="post" action="{{ url_for('submitted_form') }}">
13+
<label for="name">Name:</label>
14+
<input type="text" name="name"><br />
15+
<label for="email">Email address:</label>
16+
<input type="email" name="email"><br />
17+
<label for="site_url">Website URL:</label>
18+
<input type="url" name="site_url"><br />
19+
<label for="comments">Comments:</label>
20+
<textarea name="comments"></textarea><br />
21+
<input type="submit">
22+
</form>
23+
</div>
24+
</div>
25+
</body>
26+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<title>Submitted form</title>
4+
<link rel="stylesheet" type="text/css" href="/static/style.css">
5+
</head>
6+
<body>
7+
<div id="container">
8+
<div class="pagetitle">
9+
<h1>Form submitted</h1>
10+
</div>
11+
<div id="main">
12+
<p>Thanks for your submission, {{name}}!</p>
13+
<p>Here's a review of the information that you sent:</p>
14+
<p>
15+
<strong>Name</strong>: {{name}} <br>
16+
<strong>Email</strong>: {{email}} <br>
17+
<strong>Website URL</strong>: {{site}} <br>
18+
<strong>Comments</strong>: {{comments}}
19+
</p>
20+
</div>
21+
</div>
22+
<body>
23+
</html>

nox.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,12 @@ def run_tests_in_sesssion(
117117
if skip_flaky:
118118
pytest_args.append('-m not slow and not flaky')
119119

120-
if sample_directories is None:
121-
# session.posargs is any leftover arguments from the command line,
122-
# which allows users to run a particular test instead of all of them.
123-
if session.posargs:
124-
sample_directories = session.posargs
125-
else:
126-
sample_directories = collect_sample_dirs('.', TESTS_BLACKLIST)
120+
# session.posargs is any leftover arguments from the command line,
121+
# which allows users to run a particular test instead of all of them.
122+
if session.posargs:
123+
sample_directories = session.posargs
124+
elif sample_directories is None:
125+
sample_directories = collect_sample_dirs('.', TESTS_BLACKLIST)
127126

128127
if changed_only:
129128
changed_files = get_changed_files()

0 commit comments

Comments
 (0)
0