Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing size of columns in Flask-Admin

Is there a way to limit the size (length/width) of a ModelView column? I am using a WYSIWYG editor and this creates really long text, therefor making the column for the ModelView very long.

Here is picture of what it looks like. Look on the right hand side the last column. It is even longer than the screenshot could handle.

enter image description here

like image 902
kstullich Avatar asked Dec 10 '16 04:12

kstullich


2 Answers

Don't show the column (by exclusion):

class MyView(ModelView):

    column_exclude_list = ('description')

Don't show the column (by inclusion):

class MyView(ModelView):

    column_list = ('rating', 'category_id', 'year', 'stock', 'image')   

Reformat the column:

class MyView(ModelView):

     def _description_formatter(view, context, model, name):
        # Format your string here e.g show first 20 characters
        # can return any valid HTML e.g. a link to another view to show the detail or a popup window
        return model.description[:20]

    column_formatters = {
        'description': _description_formatter,
    }
like image 114
pjcunningham Avatar answered Oct 12 '22 22:10

pjcunningham


A way to do this could be to override the css style of the relevant column. In the Flask-admin list.html template you find the following code for creating the columns:

{% for c, name in list_columns %}
<td class="col-{{c}}">
  {% if admin_view.is_editable(c) %}
    {% set form = list_forms[get_pk_value(row)] %}
    {% if form.csrf_token %}
      {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c), csrf=form.csrf_token._value()) }}
    {% else %}
      {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c)) }}
    {% endif %}
  {% else %}
    {{ get_value(row, c) }}
  {% endif %}
</td>
{% endfor %}

So e.g. for column 2 you could add a max-width property to the css class col-2 to limit its width.

like image 45
MrLeeh Avatar answered Oct 13 '22 00:10

MrLeeh