Reducing size of columns in Flask-Admin
Asked Answered
R

2

6

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

Reynalda answered 10/12, 2016 at 4:55 Comment(0)
S
15

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,
    }
Stcyr answered 10/12, 2016 at 14:26 Comment(1)
No need to check for bounds before slicing as slice indices are handled graceful by Python. Line 6 can be shortened to return model.description[:20]Concern
C
1

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.

Concern answered 10/12, 2016 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.