Skip to content

Commit ee23e23

Browse files
committed
Refactor first three examples into modules. This will be a more sustainable structure going forward.
Change-Id: Ia601f0d4ab626501fdca2f1c4583287153b8ab70
1 parent d32b807 commit ee23e23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+921
-724
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ Dockerfile
1515
*.json
1616
*.sqlite
1717

18-
*/__pycache__/*
18+
# Ignore any Python generated files.
19+
__pycache__/
20+
*.py[cod]

flask/01-basic-app/config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
"""The Flask server configuration."""
16+
17+
import os
18+
19+
20+
class Config(object):
21+
# Note: A secret key is included in the sample so that it works.
22+
# If you use this code in your application, replace this with a truly secret
23+
# key. See https://flask.palletsprojects.com/quickstart/#sessions.
24+
SECRET_KEY = os.environ.get(
25+
'SECRET_KEY') or "REPLACE ME - this value is here as a placeholder."

flask/01-basic-app/main.py

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,20 @@
55
# use this file except in compliance with the License. You may obtain a copy of
66
# the License at
77
#
8-
# https://www.apache.org/licenses/LICENSE-2.0
8+
# http://www.apache.org/licenses/LICENSE-2.0
99
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1313
# License for the specific language governing permissions and limitations under
1414
# the License.
15-
"""The Flask server application. Defines routes.
15+
"""Entry point for the Flask server.
1616
17-
Ensure that you select an approprate run strategy at the bottom of this file.
18-
"""
17+
Loads the webapp module and starts the server."""
1918

20-
import flask
19+
from webapp import app
2120
import os
2221

23-
# Instantiate the Flask application.
24-
app = flask.Flask(__name__)
25-
26-
# Note: A secret key is included in the sample so that it works. If you use this
27-
# code in your application, replace this with a truly secret key. See
28-
# https://flask.palletsprojects.com/quickstart/#sessions.
29-
app.secret_key = "REPLACE ME - this value is here as a placeholder."
30-
31-
32-
@app.route("/")
33-
def index():
34-
"""
35-
Render the index page from the "index.html" template. This is meant to act
36-
as a facsimile of a company's home page. The Add-on Discovery URL should be
37-
set to the /classroom-addon route below.
38-
"""
39-
40-
return flask.render_template(
41-
"index.html", message="You've reached the index page.")
42-
43-
44-
@app.route("/classroom-addon")
45-
def classroom_addon():
46-
"""
47-
Renders the addon discovery page from the "addon-discovery.html" template.
48-
This is meant to be the landing page when opening the web app in the
49-
Classroom add-on iframe.
50-
"""
51-
52-
return flask.render_template(
53-
"addon-discovery.html",
54-
message="You've reached the addon discovery page.")
55-
56-
5722
if __name__ == "__main__":
5823
# You have several options for running the web server.
5924

@@ -62,18 +27,20 @@ def classroom_addon():
6227
# OAuthlib's HTTPs verification.
6328

6429
# Important: When running in production *do not* leave this option enabled.
65-
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
30+
# os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
6631

6732
# Run the application on a local server, defaults to http://localhost:5000.
6833
# Note: the OAuth flow requires a TLD, *not* an IP address; "localhost" is
6934
# acceptable, but http://127.0.0.1 is not.
70-
app.run(debug=True)
35+
# app.run(debug=True)
7136

7237
### OPTION 2: Secure localhost
7338
# Run the application over HTTPs with a locally stored certificate and key.
7439
# Defaults to https://localhost:5000.
75-
# app.run(host="localhost",
76-
# ssl_context=("localhost.pem", "localhost-key.pem"), debug=True)
40+
app.run(
41+
host="localhost",
42+
ssl_context=("localhost.pem", "localhost-key.pem"),
43+
debug=True)
7744

7845
### OPTION 3: Production- or cloud-ready server
7946
# Start a Gunicorn server, which is appropriate for use in production or a
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
"""Initialize the webapp module.
16+
17+
Starts the flask server and loads the config."""
18+
19+
from flask import Flask
20+
import config
21+
22+
app = Flask(__name__)
23+
app.config.from_object(config.Config)
24+
25+
from webapp import routes
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2021 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
"""Defines all routes for the Flask server."""
16+
17+
from webapp import app
18+
import flask
19+
20+
21+
@app.route("/")
22+
def index():
23+
"""
24+
Render the index page from the "index.html" template. This is meant to act
25+
as a facsimile of a company's home page. The Add-on Discovery URL should be
26+
set to the /classroom-addon route below.
27+
"""
28+
29+
return flask.render_template(
30+
"index.html", message="You've reached the index page.")
31+
32+
33+
@app.route("/classroom-addon")
34+
def classroom_addon():
35+
"""
36+
Renders the addon discovery page from the "addon-discovery.html" template.
37+
This is meant to be the landing page when opening the web app in the
38+
Classroom add-on iframe.
39+
"""
40+
41+
return flask.render_template(
42+
"addon-discovery.html",
43+
message="You've reached the addon discovery page.")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)