Python Forum
RuntimeError: No application found. Either work inside a view function or push an app - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: RuntimeError: No application found. Either work inside a view function or push an app (/thread-16546.html)



RuntimeError: No application found. Either work inside a view function or push an app - pascale - Mar-04-2019

Hi all
Question "How do I call "index() in "action(changePin)" to make it all work or is index not needed?
I have some code and if I # these parts i get error codes but I have a function "index()" with the right information but it is not being imported. here is my code:
from flask import render_template, flash, redirect, url_for, request, g, \
    jsonify, current_app
from flask_login import current_user, login_required
from homeHeating import db
from homeHeating.main.forms import AddPinForm
from homeHeating.models import User, Pins
from homeHeating.main import bp
import RPi.GPIO as GPIO
from homeHeating import create_app
#app = create_app()
#app.app_context().push()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

pins = Pins.query.all()
for pin in pins:
    GPIO.setup(pin.pin, GPIO.OUT)
    GPIO.output(pin.pin, GPIO.LOW)
    pin.state = GPIO.input(pin.pin)
    db.session.commit()

@bp.route('/', methods=['GET', 'POST'])
@bp.route('/index', methods=['GET', 'POST'])
@login_required
def index():
    pins = Pins.query.all()
    for pin in pins:
        GPIO.setup(pin.pin, GPIO.OUT)
        GPIO.output(pin.pin, GPIO.LOW)
        pin.state = GPIO.input(pin.pin)
        db.session.commit()
        return render_template('main/index.html', pins=pins)

@bp.route("/<changePin>/<action>")
def action(changePin, action):
    pins = Pins.query.all()
    changePin = int(changePin)
    rows = Pins.query.filter_by(pin=changePin).all()
    deviceName = rows[0].name
    #GPIO.setup(changePin, GPIO.OUT)
    if action == "on":
        GPIO.output(changePin, GPIO.HIGH)
        message = "turned "+ deviceName +" on"
    if action == "off":
        GPIO.output(changePin, GPIO.LOW)
        message = "turned "+ deviceName +" off"
    for pin in pins:
        #GPIO.setup(pin.pin, GPIO.OUT)
        pin.state = GPIO.input(pin.pin)
        db.session.commit()
    templateData = {
        'message' : message,
        'pins' : pins
    }
    return render_template('main/index.html', **templateData)
if you look through my code lines 15 to 20 are causing the problem but lines 10 and 11 if I uncoment this all works, lines 15 to 20 are needed for the function "action(changePin, action)" I want "index() to do the work of lines 15 to 20 other wise I need lines 40 and 48 (commented out) but these muck the rest of my code.
can you guys come to the rescue again?
paul Smile
EDIT following request:
Homeheating.models.Pins:
class Pins(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    pin = db.Column(db.Integer, index=True, unique=True)
    name = db.Column(db.String(64))
    upDown = db.Column(db.String(4))
    state = db.Column(db.String(9))
    
    def __repr__(self):
        return '<Pins {}>'.format(self.pin)   
Error:
app = call_factory(script_info, app_factory) File "/home/pi/heating/venv/lib/python3.7/site-packages/flask/cli.py", line 116, in call_factory return app_factory() File "/home/pi/heating/homeHeating/__init__.py", line 34, in create_app from homeHeating.main import bp as main_bp File "/home/pi/heating/homeHeating/main/__init__.py", line 5, in <module> from homeHeating.main import main File "/home/pi/heating/homeHeating/main/main.py", line 19, in <module> pins = Pins.query.all() File "/home/pi/heating/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 514, in __get__ return type.query_class(mapper, session=self.sa.session()) File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__ return self.registry() File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 1007, in __call__ return self.registry.setdefault(key, self.createfunc()) File "/home/pi/heating/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 3171, in __call__ return self.class_(**local_kw) File "/home/pi/heating/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 141, in __init__ self.app = app = db.get_app() File "/home/pi/heating/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 912, in get_app 'No application found. Either work inside a view function or push' RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.



RE: RuntimeError: No application found. Either work inside a view function or push an app - Larz60+ - Mar-04-2019

Please post any error messages you are getting in their entirety (unmodified)
Also, please supply the data structure for
homeHeating.models.Pins
This code is wrong:
for pin in pins:
    GPIO.setup(pin.pin, GPIO.OUT)
    GPIO.output(pin.pin, GPIO.LOW)
    pin.state = GPIO.input(pin.pin)
    db.session.commit()
structure should be:
for pin in pins:
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, GPIO.LOW)
    pin.state = GPIO.input(pin)
    db.session.commit()



RE: RuntimeError: No application found. Either work inside a view function or push an app - pascale - Mar-04-2019

Larz60+ Thank you so much for helping me, I have added the information you requested to the origional post.
The reason why I have written the code like this;
Quote:This code is wrong:
for pin in pins:
GPIO.setup(pin.pin, GPIO.OUT)
GPIO.output(pin.pin, GPIO.LOW)
pin.state = GPIO.input(pin.pin)
db.session.commit()
Is, before the code is a query to Pins:
pins = Pins.query.all()
for pin in pins:
    GPIO.setup(pin.pin, GPIO.OUT)
    GPIO.output(pin.pin, GPIO.LOW)
    pin.state = GPIO.input(pin.pin)
    db.session.commit()
 
each ".pin" is the column "pin" of the database, I understand it as the first pin is "for 'pin' of pins" where "pins" is now = Pins.query.all() this code works, and what I understand is that your second code would relate to a dicionary "pins".
But I have proberbly misunderstood!
Thank you for helping.
regards
Paul
Ps one day when I eventually understand this I will read as many posts as possible (except gamming) and try to help others.