Skip to content

Instantly share code, notes, and snippets.

@mrjoes
mrjoes / edit_child.html
Created October 3, 2012 11:07
Child models in flask-admin edit model view
{% extends 'admin/model/edit.html' %}
{% macro table(columns, items, endpoint) %}
<table class="table table-striped table-bordered model-list">
<thead>
<tr>
<th></th>
{% for c in columns %}
<th>{{ c }}</th>
{% endfor %}
@mrjoes
mrjoes / test.py
Created May 5, 2013 17:49
How to customize options in QuerySelectField. High level idea: 1. Hook `create_form` to change options when creating model and `edit_form` when editing model 2. Provide different `query_factory` for the field 3. Do filtering/population logic in the _get_parent_list Alternatively: 1. Can hook `scaffold_form` and change `form.parent.kwargs['query_…
from flask import Flask, request, url_for
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin.contrib import sqlamodel
from flask.ext import admin
# Create application
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
@mrjoes
mrjoes / base.py
Created March 27, 2015 00:05
CSV export for Flask-Admin
class BaseModelView(ModelView):
list_template = 'admin/model_list.html'
export_columns = None
# Exporting
def _get_data_for_export(self):
view_args = self._get_list_extra_args()
# Map column index to column name
{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}
{% block body %}
{% call lib.form_tag() %}
{{ lib.render_form_fields(form) }}
<input type="submit" />
{% endcall %}
{% endblock %}
@mrjoes
mrjoes / ckedit.py
Created March 18, 2013 19:04
Flask-Admin and CKEditor WYSIWYG textarea integration. Basically, all you have to do: 1. Create new wtforms widget which will emit 'ckeditor' class 2. Make new wtforms field which will use this widget 3. Create new jinja2 template, which includes ckeditor javascript 4. Tell flask-admin to use new field and new template
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext import admin, wtf
from flask.ext.admin.contrib import sqlamodel
app = Flask(__name__)
app.config['SECRET_KEY'] = '123456790'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite'
db = SQLAlchemy(app)
@mrjoes
mrjoes / app.py
Last active March 22, 2021 23:15
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import relationship, backref
import flask_admin as admin
from flask_admin.contrib import sqla
# Create application
app = Flask(__name__)
@mrjoes
mrjoes / demo.html
Created May 26, 2016 16:04
Flask-Admin with multiple virtual subdomains
<html>
<body>
Hello {{ subdomain }}!
<img src="{{ url_for('static', subdomain=subdomain, filename='avatar.jpeg', _external=True) }}"></img>
</body>
</html>
@mrjoes
mrjoes / INSTALL.txt
Last active February 2, 2018 21:25
Dead simple broker on top of sockjs-tornado
1. pip install -r reqs.pip
2. server.py
3. open client.html in browser
4. redis-cli publish push '123456'
5. check browser console
@mrjoes
mrjoes / async.py
Created September 12, 2012 17:51
sockjs-tornado with tornado.gen interface.
# -*- coding: utf-8 -*-
from tornado import ioloop, web, gen, httpclient
from sockjs.tornado import SockJSConnection, SockJSRouter
class IndexHandler(web.RequestHandler):
"""Regular HTTP handler to serve the ping page"""
def get(self):
@mrjoes
mrjoes / simple.py
Created September 13, 2012 13:29
How to use HTML template for list columns
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext import admin, wtf
from flask.ext.admin.model import template
from flask.ext.admin.contrib import sqlamodel
from flask.ext.admin.contrib.sqlamodel import filters
# Create application