Thursday, March 27, 2014

Use A Temporary File (StringIO) for Pandas's to_csv - Python Flask

This link helped me use a temporary file for an excel export in flask (using pandas):

Get Column and Row Labels In selectHandler - Google Charts / Visualization API

For a more complete example of using selectHandler, see this page:

My example below get the labels from the row and the columns. I used this to send the user to another link and drill-down.

// this is an example of a bar chart's selectHandler function
function selectHandler() {
var selection = chart.getSelection();
var item = selection[0];

if (item.row != null && item.column != null) {
var rowLabel = parseInt(data.getValue(item.row, 0));
var columnLabel = data.getColumnLabel(item.column);
}, 'select', selectHandler);

Saturday, March 22, 2014

Ordered Post Data - Python Requests

Here's an example of how to send ordered data with python's Requests library, for when the order of your post request data matters:

import requests
from collections import OrderedDict

payload = OrderedDict([("username", "admin"), ("pwd", "password")]) # the order matters!
r ='', data=payload)
print r.text

Monday, March 17, 2014

Multiple Views Based On Same Model - Flask-Admin

You need to manually change the endpoint like MrJoes says in issue 480:

Here's an example:
admin.add_view(MyModelView(model.CsvTable, db.session, endpoint="duplicate_view"))

You can now visit your view by going to

Tuesday, March 4, 2014

Checkmark "column_formatters" - Flask-Admin

In your custom ModelView, you need to add the following lines:
from flask.ext.admin.model.template import macro
column_formatters = dict(your_column=macro('render_your_column'))
list_template = 'list.html'

In your custom list.html template, you need to add the following jinja2 code:
{% macro render_your_column(model, column) %}
{% if model.your_column%}<i class="icon-ok"></i>{% endif %}
{% endmacro %}

If your row has a value in "your_column", then it will show as a checkmark. You need to make a new macro for each column.


Flask-Migrate is exactly what I've been looking for when it comes to database migrations. When I create PHP applications with MVC frameworks, the changes in the database need to be done manually when the model is changed. Flask-Migrate handles changes to the database (like adding/removing columns) so your models reflect your database.

This is a big advantage Flask has over PHP frameworks.