Skip to content

Commit 74293d7

Browse files
committed
Update first walkthrough to use /classroom-addon routing. Add window management JS functions. Add CSS rounded-button style.
Change-Id: Ib11ace66227c91ee588efc9fd2e61a9fba6121b8
1 parent 61a0bdd commit 74293d7

File tree

7 files changed

+119
-30
lines changed

7 files changed

+119
-30
lines changed

flask/01-basic-app/main.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,12 @@
2525

2626
@app.route("/")
2727
def index():
28-
return flask.render_template("index.html", message="Welcome!")
28+
return flask.render_template("index.html", message="You've reached the index page.")
2929

3030

31-
@app.route("/authorize")
32-
def authorize():
33-
# Set the username to test the navigation bar.
34-
flask.session["username"] = "Test Username"
35-
36-
return flask.render_template("index.html",
37-
message="Test username stored in session.")
38-
39-
40-
@app.route("/clear")
41-
def clear_credentials():
42-
flask.session.clear()
43-
44-
return flask.render_template("index.html", message="Session cleared.")
45-
31+
@app.route("/classroom-addon")
32+
def classroom_addon():
33+
return flask.render_template("addon-discovery.html", message="You've reached the addon discovery page.")
4634

4735
if __name__ == "__main__":
4836
# You have several options for running the web server.
@@ -56,14 +44,14 @@ def clear_credentials():
5644
# Run the application on a local server, defaults to http://localhost:5000.
5745
# Note: the OAuth flow requires a TLD, *not* an IP address; "localhost" is
5846
# acceptable, but http://127.0.0.1 is not.
59-
# app.run(debug=True)
47+
app.run(debug=True)
6048

6149
### OPTION 2: Secure localhost
6250
# Run the application over HTTPs with a locally stored certificate and key.
6351
# Defaults to https://localhost:5000.
64-
app.run(host="localhost",
65-
ssl_context=("localhost.pem", "localhost-key.pem"),
66-
debug=True)
52+
# app.run(host="localhost",
53+
# ssl_context=("localhost.pem", "localhost-key.pem"),
54+
# debug=True)
6755

6856
### OPTION 3: Production- or cloud-ready server
6957
# Start a Gunicorn server, which is appropriate for use in

flask/01-basic-app/static/base-style.css

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,37 @@ body {
3535
text-decoration: none;
3636
font-size: 17px;
3737
display: block;
38-
}
38+
}
3939

4040
.topnav a:hover {
4141
background-color: #ddd;
4242
color: black;
43+
}
44+
45+
a.rounded-button {
46+
display: inline-block;
47+
padding: 0.3em 1.2em;
48+
margin: 0 0.1em 0.1em 0;
49+
border: 0.16em solid rgba(255, 255, 255, 0);
50+
border-radius: 2em;
51+
box-sizing: border-box;
52+
text-decoration: none;
53+
font-family: 'Roboto', sans-serif;
54+
font-weight: 300;
55+
color: #FFFFFF;
56+
text-shadow: 0 0.04em 0.04em rgba(0, 0, 0, 0.35);
57+
text-align: center;
58+
transition: all 0.2s;
59+
background-color: #4285F4;
60+
}
61+
62+
a.rounded-button:hover {
63+
border-color: rgba(255, 255, 255, 1);
64+
}
65+
66+
@media all and (max-width:30em) {
67+
a.rounded-button {
68+
display: block;
69+
margin: 0.2em auto;
70+
}
4371
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2021 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Opens a given destination route in a new window. This function uses
19+
* window.open() so as to force window.opener to retain a reference to the
20+
* iframe from which it was called.
21+
* @param {string=} destinationURL The endpoint to open, or "/" if none is
22+
* provided.
23+
*/
24+
function openWebsiteInNewTab(destinationURL = '/') {
25+
window.open(destinationURL, '_blank');
26+
}
27+
28+
/**
29+
* Close the iframe by calling postMessage() in the host Classroom page. This
30+
* function can be called direcly when in a Classroom add-on iframe.
31+
*
32+
* Alternatively, it can be used to close an add-on iframe in another window.
33+
* For example, if an add-on iframe in Window 1 opens a link in a new Window 2
34+
* using the openWebsiteInNewTab function above, you can call
35+
* window.opener.closeIframe() from Window 2 to close the iframe in Window 1.
36+
*/
37+
function closeAddonIframe() {
38+
window.parent.postMessage(
39+
{
40+
type: 'Classroom',
41+
action: 'closeIframe',
42+
},
43+
'*');
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
Copyright 2021 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://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,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
{% extends "base.html" %}
18+
{% include "navbar.html" %}
19+
{% block content %}
20+
<div class="container">
21+
<h3>Welcome to the home page!</h3>
22+
<strong>{{message}}</strong>
23+
</div>
24+
25+
<hr>
26+
27+
<a class="rounded-button" href="#" onclick="openWebsiteInNewTab()">Visit our website</a>
28+
<a class="rounded-button" href="#" onclick="closeAddonIframe()">Close add-on iframe</a>
29+
{% endblock %}

flask/01-basic-app/templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
2727
<link rel="stylesheet" href="../static/base-style.css">
2828

29+
<script src="../static/scripts/addon-utils.js"></script>
2930
</head>
3031

3132
<body>

flask/01-basic-app/templates/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
limitations under the License.
1515
-->
1616

17-
{% extends "base.html"%}
18-
{% include 'navbar.html' %}
17+
{% extends "base.html" %}
18+
{% include "navbar.html" %}
1919
{% block content %}
2020
<div class="container">
2121
<h3>Welcome to the home page!</h3>
2222
<strong>{{message}}</strong>
2323
</div>
2424

2525
<hr>
26+
27+
<a class="rounded-button" href="#" onclick="window.opener.closeAddonIframe();window.close()">
28+
Close tab and the add-on iframe
29+
</a>
2630
{% endblock %}

flask/01-basic-app/templates/navbar.html

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@
1717
<div class="topnav">
1818
<ul>
1919
<li>
20-
<a href="{{url_for('index')}}">Home</a>
20+
<a href="{{url_for('index')}}">Company Homepage</a>
2121
</li>
2222

2323
<li style="float: right">
24-
{% if session['username'] %}
25-
<a style="background-color: #db4437" href="{{url_for('clear_credentials')}}">Logout
26-
{{ session['username'] }}</a>
27-
{% else %}
28-
<a style="background-color: #4285f4" href="{{url_for('authorize')}}">Login</a>
29-
{% endif %}
24+
<a style="background-color: #4285f4" href="#">Login Placeholder</a>
3025
</li>
3126
</ul>
3227
</div>

0 commit comments

Comments
 (0)