Last updated: 2019-09-06
Got a Flask app you want to run on a Raspberry Pi in kiosk mode? Here's how.
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 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
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.
Auto-mounting a USB stick is pretty easy:
- install usbmount:
sudo apt install usbmount
- create a
symbolic linkbetween the actual upload location and the place that Flask thinks your uploads should be. This is a really simple thing to do:
- locate the uploads. In my case they were at:
- locate the place Flask is looking for them. For me this was
- if the folder exists remove it using
- create a symbolic link between the two
ln -s /my_app/static/images ~/my_app/my_app/static/images. The
-smakes 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
. /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
.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 and go make yourself a $beverage – mines a black coffee, ta :).
this is optional ↩︎