Jsonify doesn't seem to work out of application context, is there a workaround?
I am replacing some ajax requests with websockets because it is necessary for performance and network problems. I installed Flask-WebSocket with pip in my env. Now I get the error message:
RuntimeError: working outside of application context
The skeleton of my application is as follows:
app/
βββ forms
βββ static
β βββ css
β βββ img
β β βββ DefaultIcon
β β βββ eps
β β βββ png
β βββ js
βββ templates
βββ ups
βββ views
The python files for websockets are located in views / ajax.py:
import time
from flask import jsonify
from .. import sockets
from app.functions import get_cpu_load, get_disk_usage, get_vmem
from app import app
from app.views.constants import info, globalsettings
@sockets.route('/_system')
def _system(ws):
"""
Returns the system informations, JSON Format
CPU, RAM, and Disk Usage
"""
while True:
message = ws.receive()
if message == "update":
cpu = round(get_cpu_load())
ram = round(get_vmem())
disk = round(get_disk_usage())
ws.send(jsonify(cpu=cpu, ram=ram, disk=disk)
I run my application using the following command:
gunicorn -k flask_sockets.worker app:app
Here is mine __init__.pyin the app / folder:
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
app.config.from_object('config')
from app import views as application
Why doesn't jsonify work, what can I use instead?