Paul's Programming Notes PostsRSSGithub

Create and edit records in a modal - Flask-Admin

By default Flask-Admin sends you to a separate page to create or edit a record. I added options to do it in a modal on top of the list instead.

class UserView(ModelView):
    create_modal = True
    edit_modal = True

Set those and the New and Edit buttons open the form in a popup, submit through the normal flow, and drop you back on the list. Edit came first (#919), then create (#925). Getting the create modal right meant fixing a few things: missing CSS/JS when a submit came back with validation errors, the Bootstrap 3 backdrop showing through, and translations for the modal header.

I also added a read-only details view and moved the modal markup into its own templates so the create, edit, and details modals could share it (#942), and wired the same modals into FileAdmin (#933).

Docker Run In Crontab

0 5 * * 1 docker run --rm --name=mycontainer ubuntu:13.10 /opt/bin/job

The above command is an example of how “docker run” would be used in crontab to run once every week at 5am.

  • --rm will delete the container once the job is finished running
  • --name will name the container and prevent duplicate jobs from running
  • You don’t need to use “&” at the end, because crons already run in the background.

MySQL Batch Updates Not Working

Looking at “SHOW PROCESSLIST” and it looks like your queries are running individually instead of in batches like you sent?

This happens because MySQL runs each update statement individually, but you should still be able to see the batches when the queries are in the “init” state.

Use this query to see the batched queries:

SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST where state="init";

Searching non-text columns - Flask-Admin

Flask-Admin’s search box only worked on text columns. I had a ModelView showing usernames next to a related number, and to find a row by that number you had to set up a filter instead of just typing it into the search box.

So I loosened the search to accept non-text SQLAlchemy columns too (integers, dates, and so on), using ILIKE, which casts and matches across those types (#795). I added tests for searching integer columns on SQLite and MySQL, across SQLAlchemy 0.7 and 0.9.8.

class UserView(ModelView):
    column_searchable_list = ['username', 'account_number']  # account_number is an integer

mod_wsgi + Flask

Here’s my mod_wsgi configuration for a flask app that uses Flask-Admin.

Trailing slashes matter! If you set a trailing slash, it will only set the alias for that directory and not all of the directories below it.