Search code examples
pythonflaskflask-admin

Flask Admin: TextArea with monospace font


I have a database table with a column of type 'Text'. Flask-Admin displays this text in a TextArea field and allows me to edit it. However, I would like the text to display in monospace font. How can I do this?

I know that I can influence the widgets created by Flask-Admin by creating view classes. E.g. to increase the height of the text area I do:

class MyView(sqla.ModelView):
    form_widget_args = {'description' : {'rows' : 100}}

(where 'description' is the name of my text column). Is there an equivalent to the 'rows' keyword that lets me switch to monospace font? I couldn't find a comprehensive list of allowed keywords.


Solution

  • The "allowed keywords" of the widget arguments dictionary are simply the various HTML attributes that can be specified for the input field. That being said, attributes like style (shown below) are valid attributes for all HTML tags.

    You should be able to accomplish what you want by specifying the style of the form widget to use a monospace font.

    form_widget_args = {
        'description': {
            'rows': 100,
            'style': 'font-family: monospace;'
        }
    }
    

    Within the style field, you can specify any valid CSS that you would like to apply to the widget just like you would in standard HTML. The HTML equivalent here would be:

    <textarea rows="100" style="font-family: monospace;"></textarea>
    

    If you really want to be comprehensive in specifying a font family, you could do the following which would search for all the listed fonts prior to defaulting to the system monospace font.

    form_widget_args = {
        'description': {
            'rows': 100,
            'style': 'font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;'
        }
    }