I am trying to modify this Flask-admin example code http://examples.flask-admin.org/forms/admin/image/ to display both the thumbnail of the image and the raw text of the file path as two separate columns. This is basically the same problem I am trying to solve on my own project. I can make it display only one of them, but not both at the same time.
The relevant class in the example source code is:
class ImageView(sqla.ModelView):
def _list_thumbnail(view, context, model, name):
if not model.path:
return ''
return Markup('<img src="%s">' % url_for('static', filename=form.thumbgen_filename(model.path)))
column_formatters = {
'path': _list_thumbnail
}
My problem is that the ModelView class includes columns to display by referencing them by their name in the SQAlchemy Model (e.g. 'name' or 'path'). If I want it to have both the path and the thumbnail then I need to include column 'path' twice. However I cannot apply different formatter functions to each of the 'path' columns. So I can have two thumbnail columns or two text path columns only.
I tried to trick it by labelling the two 'path' columns differently with column_labels
, but this is a dictionary object not a list so again you can't discriminate between the two.
(full source here: https://github.com/flask-admin/flask-admin/blob/master/examples/forms/app.py)
You are close. Define a "dummy" column in the view's column_list
property, give it a nice label via the column_label
property and now you can use the "dummy" column in column_formatters
property.
For example:
class ImageView(sqla.ModelView):
# Add all your visible columns here include a "dummy" column - in this instance 'raw_path'
column_list = ('path', 'raw_path')
# Define label for dummy column
column_labels = {
'raw_path': 'Full Path'
}
def _list_thumbnail(view, context, model, name):
if not model.path:
return ''
return Markup('<img src="%s">' % url_for('static', filename=form.thumbgen_filename(model.path)))
def _list_raw_path(view, context, model, name):
if not model.path:
return ''
return Markup('<p>{path}</p>'.format(path=model.path))
column_formatters = {
'path': _list_thumbnail
'raw_path': _list_raw_path
}