Skip to content

Instantly share code, notes, and snippets.

@xtekky
Created November 3, 2022 19:57
Show Gist options
  • Save xtekky/d918840cfcc6733eba68e214380a498d to your computer and use it in GitHub Desktop.
Save xtekky/d918840cfcc6733eba68e214380a498d to your computer and use it in GitHub Desktop.
Flask ready to go boilerplate
# by @io
import secrets
import os
from flask import (
Flask,
render_template,
send_file
)
app = Flask(__name__, template_folder='assets/html')
app.config['SECRET_KEY'] = secrets.token_hex(64)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
class website:
def __init__(self) -> None:
self.routes = {
'/': {
'function': self._index,
'methods': ['GET', 'POST']
},
'/assets/<folder>/<file>': {
'function': self._assets,
'methods': ['GET', 'POST']
}
}
self.config = {
'host': '0.0.0.0',
'port': 5000 if os.name == 'nt' else 80,
'debug': True
}
def _index(self):
return render_template('index.html')
def _assets(self, folder: str, file: str):
try:
return send_file(f"assets/{folder}/{file}", as_attachment=False)
except:
return "File not found", 404
if __name__ == '__main__':
website = website()
for route in website.routes:
app.add_url_rule(
route,
view_func=website.routes[route]['function'],
methods=website.routes[route]['methods']
)
app.run(**website.config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment