Running a Flask App on a Raspberry Pi in kiosk mode

Setting up a Flask app to run on a Raspberry Pi in Kiosk Mode

Last updated: 2019-09-06

Got a Flask app you want to run on a Raspberry Pi in kiosk mode? Here's how.

Setup

Set up your Raspberry Pi to work in kiosk mode - I usually set it to go to Google or the BBC News, something I know is unlikely to fall over while I'm testing. Once you know it is working rename .xsession to xsession so that it doesn't run automatically. This allows you to boot your pi normally and check that your app is working before you setup all the auto-booting magic.

Transfer your Flask App on to your pi.

Virtualenv and flask app installation

While Virtualenv isn't strictly necessary it's still good practice so lets grab that and set up a venv for our app

Install Virtualenv using either sudo apt-get install virtualenv or pip install virtualenv.

Jump into your project folder cd my/project/folder and create a Virtualenv:

virtualenv -p python3 venv If you want a python2.* environment then you can miss out the -p python3.

Activate your Virtualenv:

cd venv source ./bin/activate

Install your Flask app either using pip install your_flask_app or by installing it and it's dependencies. NOTE: If you're using bcrypt you will first need to sudo apt-get install python3-dev otherwise it will fail. If it still fails you may also need sudo apt-get install libffi-dev.

Uploads

Auto-mounting a USB stick is pretty easy:

  1. install usbmount: sudo apt install usbmount
  2. modify /lib/systemd/system/systemd-udevd.service so that PrivateMounts=yes reads PrivateMounts=no[1]
  3. reboot
  1. create a symbolic link between the actual upload location and the place that Flask thinks your uploads should be. This is a really simple thing to do:
  2. locate the uploads. In my case they were at: my_app/static/images
  3. locate the place Flask is looking for them. For me this was ~/my_app/my_app/static/images
  4. if the folder exists remove it using rm -r
  5. create a symbolic link between the two ln -s /my_app/static/images ~/my_app/my_app/static/images. The -s makes this a soft link as you can't hard link folders only files.

Now your app should be able to see your uploaded files. Start your app to make sure everything's working.

Auto-start Virtualenv and Flask

There are a variety of ways to do this: chron (Running A Python Script At Boot Using Cron), daemons (see Getting a Python script to run in the background (as a service) on boot and http://raspberrypi.stackexchange.com/questions/12580/run-python-flask-server-from-daemon), rc.local.

Partly because cron was failing me (and I couldn't work out why) I went with the second simplest solution which was adding the following to /etc/rc.local before exit 0:

. /home/pi/PATH_TO_VENV]/bin/activate
python /home/pi/PATH_TO_FILE_THAT_STARTS_APP.py

Reboot your pi and navigate to http://127.0.0.1:5000/ to check that everything works (or just skip this step if you're super confident).

Auto-start Chromium in Kiosk mode

Put your .xsession file back: mv xsession .xsession Then edit it so it's hitting http://127.0.0.1:5000/ rather than whichever random site you told it to go to before. Reboot one more time just to prove to yourself that everything is absolutely working. Do a dance[2] and go make yourself a $beverage – mines a black coffee, ta :).


source: https://raspberrypi.stackexchange.com/a/100375 ↩︎

this is optional ↩︎