How to snap a simple flask application

Is there a good example or tutorial on how to snap a simple python3 Flask application?

It seems to be a simple task which despite this has me caught in lots of work and perils.

i dont think there is any tutorial, but i happen to recently have started an opencv demo that is embedded in a HTML page and served through flask here:

perhaps that gives you some hints …

So, I’ve finally - after massive issues all over - been able to produce a simple example snap containing a flask application.

The process is as follows:

  1. Produce the charmcraft.yaml
  2. Use the python plugin to install flask - Make it a part.
  3. Use the dump plugin to place the python-file with your flask app - Make it a second part.
  4. Use “organize” to copy the file to a place available to your command, e.g. bin/ or so.
  5. Create the command/service.
name: microsample
version: git
summary: A simple flask microservice on 0.0.0.0:8080
description: |
  Get service status with: curl http://hostip:8080/  (Will return "Online" if OK)
grade: devel # must be 'stable' to release into candidate/stable channels

confinement: strict # use 'strict' once you have the right plugs and slots

# This helps, for example, build.snapcraft.io to build only on archs we can run on.
architectures:
  - build-on: amd64

base: core22

apps:
  microsample:
    command: bin/flask run -h 0.0.0.0 -p 8080
    daemon: simple
    restart-condition: always
    plugs: [network-bind]
    environment:
      FLASK_APP: ./server.py
      LC_ALL: C.UTF-8
      LANG: C.UTF-8

parts:

# This installs flask using python pip.
  python-flask:
    plugin: python
    source: .
    python-packages: [ flask ]
    
# This dumps our sources including server.py
# where we manually have to copy the server.py
# into place.
  flask-server:
    plugin: dump
    source: .
# flask could be installed like this instead in the plugin.
#    stage-packages: [ python3-flask ]
    organize:
      server.py: bin/

2 Likes