Skip to content

Instantly share code, notes, and snippets.

@danlurie
Created December 13, 2016 07:15
Show Gist options
  • Save danlurie/3ec7fe943dda28d16579210a92bcdfbe to your computer and use it in GitHub Desktop.
Save danlurie/3ec7fe943dda28d16579210a92bcdfbe to your computer and use it in GitHub Desktop.
from flask import render_template, flash
from flask.ext.appbuilder.models.sqla.interface import SQLAInterface
from flask.ext.appbuilder import ModelView, expose, BaseView, has_access, SimpleFormView
from app import appbuilder, db
from wtforms import StringField
from wtforms.validators import DataRequired
from flask.ext.appbuilder.fieldwidgets import BS3TextFieldWidget
from flask.ext.appbuilder.forms import DynamicForm
from .compute import test_rand
from .models import Patient
"""
Create your Views::
class MyModelView(ModelView):
datamodel = SQLAInterface(MyModel)
Next, register your Views::
appbuilder.add_view(MyModelView, "My View", icon="fa-folder-open-o", category="My Category", category_icon='fa-envelope')
"""
"""
Example views from http://flask-appbuilder.readthedocs.io/en/latest/views.html
"""
class Home(BaseView):
default_view = 'view_overlap'
@expose('/view_overlap/')
@has_access
def view_overlap(self):
self.update_redirect()
return self.render_template('view_overlap.html')
appbuilder.add_view(Home, 'View Overlap', category='Home')
class MyForm(DynamicForm):
field1 = StringField(('Field1'),
description=("We're number one!"),
validators=[DataRequired()],
widget=BS3TextFieldWidget())
field2 = StringField(('Field2'),
description=("We're number two! (And we're optional)"),
widget=BS3TextFieldWidget())
class MyFormView(SimpleFormView):
form = MyForm
form_title = "Form View MKI"
message = "It worked!"
def form_get(self, form):
# pre-process form
form.field1.data = 'I dunno man, I just woke up here.'
def form_post(self, form):
# post-process form
flash(self.message, 'Success!')
appbuilder.add_view(MyFormView, "My Form View", icon='fa-group', label='My form View',
category='My Forms', category_icon='fa_cogs')
"""
BDPDB Views
"""
class PatientView(ModelView):
datamodel = SQLAInterface(Patient)
list_columns = ['patient_number', 'dob', 'sex', 'lesion_location', 'lesion_cause']
add_columns = ['patient_number', 'dob', 'sex', 'lesion_location', 'lesion_cause',
'lesion_date', 'referral_site', 'mask_path']
edit_columns = ['patient_number', 'dob', 'sex', 'lesion_location', 'lesion_cause',
'lesion_date', 'referral_site', 'mask_path']
show_fieldsets = [
('Patient Info', {'fields':['patient_number', 'dob', 'sex', 'lesion_location',
'lesion_cause', 'lesion_date', 'referral_site', 'mask_path']})
]
db.create_all()
appbuilder.add_view(PatientView, "List Patients", icon='fa-users',category='Patients')
"""
Application wide 404 error handler
"""
@appbuilder.app.errorhandler(404)
def page_not_found(e):
return render_template('404.html', base_template=appbuilder.base_template, appbuilder=appbuilder), 404
db.create_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment