Skip to content

Instantly share code, notes, and snippets.

Last active Sep 12, 2018
What would you like to do?
sample single file templateless flask application that demonstrates flask_bootstrap, flask_wtform, flask_nav, flask_login, and dominate
sample single file templateless flask application that demonstrates flask_bootstrap, flask_wtform, flask_nav, flask_login
uses dominate instead of templates
to use: (in virtual env)
pip install dominate Flask Flask-Bootstrap Flask-WTF flask-nav Flask-Login
from flask import Flask, session, request, redirect, render_template_string, flash
from flask.sessions import SecureCookieSessionInterface
from flask_bootstrap import Bootstrap,bootstrap_find_resource, render_form
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user , current_user
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import DataRequired
import dominate as dom
import dominate.tags as t
from dominate.util import raw
from flask_nav import Nav
from flask_nav.elements import Navbar, View, Subgroup, Link, Text, Separator
from functools import wraps
app = Flask(__name__)
app.secret_key = "rAxIqk4mMZkc6VYWwndDDCNg5D6q0AUDs11pGENoNoeo3V1SWVebuOO5XizEevDxl3wVQHRDnp7skeoU"
app.session_interface = SecureCookieSessionInterface();
# login form
class LoginForm(FlaskForm):
login = StringField('Login', validators=[DataRequired()])
passwd = PasswordField('Passwd', validators=[DataRequired()])
submit_button = SubmitField('Login')
# dominate tag which has no tag. useful for example to add js script at end of body
class noTag(t.html_tag):
def _render(self, sb, indent_level=1, indent_str=' ', pretty=True, xhtml=False):
pretty = self._render_children(sb, indent_level , indent_str, pretty, xhtml)
## flask form template rendering (render_form is unfortunately not finished)
{%% import "bootstrap/wtf.html" as wtf %%}
{{ wtf.quick_form(form, form_type='%s'%s) }}
def renderForm(form, formType='basic', extraArg='') :
return raw(render_template_string(formTemplate%(formType,extraArg),form=form))
### nav
barGroup = Subgroup( # static subgroup
Link('Flask-Bootstrap', ''),
Link('Flask-AppConfig', ''),
Link('Flask-Debug', ''),
Link('Getting started', ''),
Link('Layout', ''),
Link('Content', ''),
Link('Components', ''),
Link('Utilities', ''),
Link('About', ''),
def topBar():
# dynamic nav bar
return Navbar(*(
[ View('Flask-Bootstrap', '.hello')
, View('Dashboard', '.dash')
+ [ View('Logout', '.logout')] * current_user.is_authenticated
+ [ Text('Welcome %s'%('guest' if not current_user.is_authenticated else
, barGroup
## flask_login minimal user class
class User(UserMixin):
def __init__(self,id): = id
login_manager = LoginManager()
login_manager.login_view = "login"
def load_user(userid):
return User(userid)
# this is our bootstrap document template
def getDoc() :
doc = dom.document(title="mypage")
with doc.head:
t.meta(name="viewport",content="width=device-width, initial-scale=1.0")'css/bootstrap.css', cdn='bootstrap'),rel="stylesheet")
doc.body, doc.bodyEnd = doc.body.add(t.div(_class="container"), noTag(t.comment("bodyEnd")))
with doc.body:
# TBD: display flash messages
with doc.bodyEnd:
t.script(src=bootstrap_find_resource('jquery.js', cdn='jquery'))
t.script(src=bootstrap_find_resource('js/bootstrap.js', cdn='bootstrap'))
return doc
#decorator that calls our template
def stdDoc(func):
def wrap():
with doc.body:
if r is not None:
return r
return doc.render()
return wrap
def login():
form = LoginForm(request.form)
if request.method=='POST' and form.validate_on_submit(): # to get error messages to the browser
if ==
flash("Login Ok!")
return redirect(request.args.get("next"))
form.passwd.errors=["wrong password"]
with t.div(_class="jumbotron"):
t.h1("Please login")
t.p("Password must equal to login in this example")
renderForm(form, formType='horizontal')
def logout():
logout_user() # call logout_user before getDoc to update the navbar
with doc.body:
t.h1("Logout done")
return doc.render()
def dash():
t.h1("Welcome %s"
def hello():
if __name__ == '__main__':, host="")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment