Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask without Flask
#1
Hi,

I have been writing a fairly extensive home automation system in Python (and C++ for microcontrollers). I have it all working and in operation for it's first version. Still lots to do on it, but the next step is providing monitoring and control... aka UIs.

One option is to migrate all the in-memory data into a central service layer and put a REST API on that. However this adds a huge amount of complexity in moving data around. So...

I thought I would do was to add REST APIs to the various components. The relevant components currently are:

Data Hub
Scheduler (including currently the Demand Processor)
Heating Controller
Lighting Controller

These all currently operate on in memory data in dictionaries. Data includes:

Data Hub: All "datum" items such as temperatures, presence flags, control statuses (eg. heating on). The "demands" dictionary, including heating, lightening demands and presence detection outputs. (Note a demand is not the same as the status datum though they are often 1 to 1).
Scheduler: Schedule start/stop/day of week, target temperatures, demand processor config
Heating Controller: Very little, bar the IP of the heating wifi switch.
Lighting Controller: The IPs of the grouped lights and their demand "key", is groupLights1

The top two are badly in need of UIs. Use cases would be things like:
* Raise a demand for living groupLights1
* Raise a demand to force the heating ON/OFF for 1 hour
* View the temperatures
* View/change the target temperatures.
* Change the start time of the weekday morning schedule.
* Save/Reload state - not currently implemented.

As I don't mind bookmarking REST URIs I can see this as the best place to start and can work on actual web UIs later once I have these.

So, Flask was suggested by a few people to me and looking through it's documentation it seemed great until I seen how you run it. Then I exploded into a rant. "WHY? Is it because the people who developed Flask thought the world revolved around them and their framework? Why do you need to run "flash.py" and point to your application? This is completely brittle and absolutely infuriating.", then I calmed down a bit.

There has to be a way to launch and update the flask conponents from within another application. I am absolutely NOT prepared to give up my __main__ method as it contains, in some cases a load of threaded services which need shutdown hooks and some critical timing updates.

Is it possible to set up the Flask context without having flask run the __main__ method or is there an equivalent of relevant framework that I can hang REST APIs of existing scripts?

Thanks
Paul
Reply
#2
download the source and have a look: https://github.com/pallets/flask
Reply
#3
Hi,

@venquessa : there seems to be a misunderstanding from your side: Flask is a WSGI-compliant (=Python's standard) framework, so the app written in Flask can be servered by any WSGI server. This could be the build-in server of Flask, but it could also be any WSGI server. E.g. gunicorn is a popular choice.

Running a Flask app with an external WSGI server is described in Flask's documentation.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask request object None muckypup 0 2,152 Mar-11-2021, 12:34 PM
Last Post: muckypup
  Modify XML request on Flask with ElementTree Chris_SK 0 2,441 Oct-26-2020, 08:59 AM
Last Post: Chris_SK
  500 Internal Server Error Flask Api on Lighttpd koklimabc 0 2,235 Apr-14-2020, 08:15 AM
Last Post: koklimabc
  Flask and Websockets == True? rxndy 2 2,783 Apr-21-2019, 04:08 PM
Last Post: rxndy
  Flask.socketio and client side html tables / JS georgelza 1 4,004 Jun-05-2018, 03:56 AM
Last Post: georgelza
  flask Skaperen 6 8,719 Apr-19-2017, 06:03 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020