-
I'm running Home-Assistant-Core as a docker container on my Unraid server. I want to move my simple home flask server from a standalone PC and into the I started with a simple flask app script and it appears to run since I don't see any errors in the logs after I installed all the dependencies, but I don't know if it's actually running in the background. I did a What do I need to do to get my flask script to run as a service in the background all the time? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
I'm not sure why you don't just run a standalone Flask Service for this. However, regardless of that, you'd have to show some code for me to even be able to start to figure out why it's not working. Please keep in mind, pyscript is not "pure python". Therefore, a lot of things, especially when dealing with classes and native modules, have to be done a bit differently than you would do in plain python. Also, if you don't want a standalone Flask service, have you considered implementing this as a Home Assistant webhook? |
Beta Was this translation helpful? Give feedback.
-
I don't know what a standalone Flask Service is within Home Assistant. My script has a lot of custom functions and calls to LAN devices and URLs and local files so I wanted everything in one place and in one file. I used to be able to run The test code looks like this: from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__, static_url_path='')
api = Api(app)
# responds to /hello endpoint
class Hello(Resource):
def hello_world():
return 'Hello World'
api.add_resource(Hello, '/hello')
if __name__ == '__main__':
app.run(host='0.0.0.0', port='5002', debug=True) My pyscript:
allow_all_imports: true
hass_is_global: true |
Beta Was this translation helpful? Give feedback.
-
The above code will not work in pyscript as-is, because name will never be "main". So you've got quite a bit of homework to do if you want to go down this road. A standalone Flask service would be running Flask outside of Home Assistant, pyscript, or anything else. Just plain old python and Flask. You can connect to Home Assistant via websocket if you need to read/set entities or call services. |
Beta Was this translation helpful? Give feedback.
-
I was hoping to keep everything self-contained within Home Assistant. It seems that's not feasible. So why wouldn't it work if I just removed the |
Beta Was this translation helpful? Give feedback.
-
The Documentation for run() indicates that it should only be used for developmental purposes. On top of that, it Blocks. When you reach this line of code, Python will stop executing any lines of code after it. And, because pyscript runs in Home Assistant's event loop, it will Block all of Home Assistant too. There are other ways to provide HTTP endpoints in Python. If you really want to pursue this (instead of standalone Flask, or Home Assistant webhooks) you should look into other Python HTTP Server options (like aiohttp, for example). |
Beta Was this translation helpful? Give feedback.
-
Using pyscript to run a Flask Python API endpoint is unsupported. |
Beta Was this translation helpful? Give feedback.
Using pyscript to run a Flask Python API endpoint is unsupported.