Skip to content

Commit

Permalink
pwa #1
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaan7 committed Jul 4, 2019
1 parent 0ee48ef commit 1781442
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 29 deletions.
101 changes: 101 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
This simple flask app is to help calculate GPA for GGSIPU University students.
### https://ipugpacalculator.herokuapp.com/

<p align="center"><img src="static/favicon.ico" width="300" height="300" border="1px" /></p>
<p align="center"><img src="static/images/favicon.ico" width="300" height="300" border="1px" /></p>
16 changes: 16 additions & 0 deletions application.py → app/application.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import os
from flask import Flask, render_template, request, redirect, url_for, session

app = Flask(__name__)
app.secret_key = "66b58fafa6a470f26fd2adc9de14cef2"

''' PWA Stuff '''
# only trigger SSLify if the app is running on Heroku
if 'DYNO' in os.environ:
from flask_sslify import SSLify
sslify = SSLify(app)

@app.route('/sw.js', methods=['GET'])
def sw():
return app.send_static_file('sw.js')

''' Routes/views '''

@app.route('/', methods=['GET','POST'])
@app.route('/home', methods=['GET','POST'])
def index():
no_of_subjects = 14
if request.method == 'POST':
Expand All @@ -13,6 +27,8 @@ def index():
return render_template('index.html', no_of_subjects=no_of_subjects, FinalGPA=FinalGPA)
return render_template('index.html', no_of_subjects=no_of_subjects)

''' Utility functions '''

def gpa_calc(no_of_subjects, credits, grades):
FinalGPA = 0
grade_dict = { 'O': 10, 'A+': 9, 'A': 8, 'B+': 7, 'B': 6, 'C': 5, 'P': 4 }
Expand Down
File renamed without changes.
Binary file added app/static/images/Icon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-152.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-36.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-72.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/images/Icon-96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
45 changes: 45 additions & 0 deletions app/static/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(function() {
if('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('../sw.js')
.then(function(registration) {
console.log('Service Worker Registered');
return registration;
})
.catch(function(err) {
console.error('Unable to register service worker.', err);
});
navigator.serviceWorker.ready.then(function(registration) {
console.log('Service Worker Ready');
});
});
}
})();

let deferredPrompt;
const btnAdd = document.querySelector('#btnAdd');

window.addEventListener('beforeinstallprompt', (e) => {
console.log('beforeinstallprompt event fired');
e.preventDefault();
deferredPrompt = e;
btnAdd.style.visibility = 'visible';
});

btnAdd.addEventListener('click', (e) => {
btnAdd.style.visibility = 'hidden';
deferredPrompt.prompt();
deferredPrompt.userChoice
.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
} else {
console.log('User dismissed the A2HS prompt');
}
deferredPrompt = null;
});
});

window.addEventListener('appinstalled', (evt) => {
app.logEvent('app', 'installed');
});
58 changes: 58 additions & 0 deletions app/static/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "IPU GPA Calculator",
"short_name": "IPU GPA Calculator",
"description": "This simple flask PWA is to help calculate GPA/Grade for GGSIPU University students.",
"theme_color": "#000",
"background_color": "#fff",
"display": "standalone",
"orientation": "portrait",
"scope": "/",
"start_url": "/",
"icons": [
{
"src": "images/icons/icon-36x36.png",
"sizes": "36x36",
"type": "image/png"
},
{
"src": "images/icons/icon-48x48.png",
"sizes": "48x48",
"type": "image/png"
},
{
"src": "images/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "images/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "images/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "images/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "images/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
},
{
"src": "images/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "images/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
11 changes: 11 additions & 0 deletions app/static/offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Offline!</title>
</head>
<body>
<h1>It seems you might be offline!</h1>
<p><a href="/">Go back to home page </a></p>
</body>
</html>
51 changes: 51 additions & 0 deletions app/static/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
self.addEventListener("install", function(event) {
event.waitUntil(preLoad());
});

var preLoad = function(){
console.log("Installing web app");
return caches.open("offline").then(function(cache) {
console.log("caching index and important routes");
return cache.addAll(["/","/static/js/app.js", "/static/css/main.css", "/static/images/favicon.ico","/home", "/offline.html"]);
});
};

self.addEventListener("fetch", function(event) {
event.respondWith(checkResponse(event.request).catch(function() {
return returnFromCache(event.request);
}));
event.waitUntil(addToCache(event.request));
});

var checkResponse = function(request){
return new Promise(function(fulfill, reject) {
fetch(request).then(function(response){
if(response.status !== 404) {
fulfill(response);
} else {
reject();
}
}, reject);
});
};

var addToCache = function(request){
return caches.open("offline").then(function (cache) {
return fetch(request).then(function (response) {
console.log(response.url + " was cached");
return cache.put(request, response);
});
});
};

var returnFromCache = function(request){
return caches.open("offline").then(function (cache) {
return cache.match(request).then(function (matching) {
if(!matching || matching.status == 404) {
return cache.match("offline.html");
} else {
return matching;
}
});
});
};
44 changes: 44 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>

<head>
<!-- PWA -->
<link rel="manifest" href="{{ url_for('static', filename='manifest.json') }}">
<!-- "Metadata. The story behind the data" ~ Elliot Alderson -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="description" content="GGSIPU GPA/Grade Calculator">
<meta name="keywords" content="GGSIPU, IPU, grade, cgpa, Eshaan Bansal, gpa, gpa calculator">
<meta property="og:description" content="GGSIPU GPA/Grade Calculator">
<meta property="og:title" content="IPU GPA Calculator">
<meta property="og:type" content="website">
<!-- Favicon -->
<link rel='shortcut icon' type='image/x-icon' href="{{ url_for('static', filename='images/favicon.ico') }}"/>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<!-- local css -->
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/main.css') }}"/>
{% block head %}{% endblock %}
<!-- Title -->
<title>IPU GPA Calculator</title>
</head>

<body>

{% block body %} {% endblock %}
<!-- PWA stuff -->
<script type="text/javascript" src="{{ url_for('static', filename='js/app.js') }}"></script>
<script type="text/javascript">
if (!navigator.serviceWorker.controller) {
navigator.serviceWorker.register("/sw.js").then(function(reg) {
console.log("Service worker has been registered for scope: " + reg.scope);
});
}
</script>

</body>

</html>
File renamed without changes.
Loading

0 comments on commit 1781442

Please sign in to comment.