Python Flask

From Wildsong
Jump to navigationJump to search

I have been noodling around with Flask for a few years now.

I decided I should start collecting information in one place.

Older pages include Flask Login and a few others, search for Flask. Mostly references to projects.


After writing a few little projects, I got tired of repeating myself so I wrote this Flask Template

Then I realized what I really want is a Flask microservices backend and a Svelte front end. So I am writing this Flask API Template; it's so new I don't even know for sure what it does yet. Check the README there.

Flask-restx

Flask-restx is the descendant of flask-restful which is a module to help you write APIs and microservices.

Getting flask-restx to load

I decided to try it and found I could not install it. Here are some notes on what I did to get flask-restx working for me. The problem: it would install pytz as a dependency and that triggered some weird shim thing that would install and then apparently disable pytz.

There is an issue in flask-restx github repo suggesting it was easy to remove pytz. So I forked it and changed these files. Turns out this part was easy. See my fork at github; look at the pytz_remove branch

modified:   examples/zoo_app/requirements.txt

 modified:   flask_restx/inputs.py

modified:   requirements/install.pip
modified:   tests/test_inputs.py

Mostly this means changing pytz.utc or pytz.UTC to timezone.utc

In inputs.py, there is some code that sets timezone on naive times to UTC using pytz "localize" but it appears to do nothing special since it's

setting the timezone to utc and input is assumed to be in utc already. The astimezone(utc) method called in there does the same time for naive times.


Then I needed to install the fixed version, I have never done this before. To test the changes I did this

conda create --name=tz pythyon autopep8 jupyter flask

Normally a conda install would find the dependencies (aniso8601 and jsonschema), but this is not a normal situation. I installed them explicitly then installed the modified flask-restx package without deps, as was recommmended.

conda activate tz (or whatever your environment is)
conda install aniso8601 jsonschema
cd ~/Documents/source/flask-restx
pip install --no-build-isolation --no-deps -e .

If you do "conda list" you should now see flask-restx in there just like any other package. There is a thing called "conda develop" that is supposed to help here but search and you will find dismal commentary on how bad it is; that's where I found the suggestion to use the "pip" command. I am hoping the maintainers of flask-restx pick up on the changed so that I don't have to learn more about developing Python modules for redistribution.

Testing flask-restx

Now I need a super super simple example that I can load to test it. Maybe I should read this guy's blog posting. https://github.com/reritom/Flask-Microservice-Tutorial Excuse me, be right back. Okay, here is the code for my test; thanks for your patience. Run it in my VSCode environment or possibly with "FLASK_APP=app.py flask run" after you save it to app.py. It returns the current environment as JSON when you hit http://localhost:5000/env

import os
from flask import Flask
from flask_restx import Api, Resource
app = Flask(__name__)
api = Api(app)
 
@api.route("/env")
class HelloEnvironment(Resource):
   def get(self):
       return dict(os.environ)