Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 RuntimeError: No application found. Either work inside a view function or push an app
#1
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/.
Quote
#2
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()
Quote
#3
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas .rolling() with some calculations inside irmscher 5 459 Apr-04-2019, 11:55 AM
Last Post: scidam
  Python - Database for Machine Learning application braveYug 2 477 Mar-04-2019, 10:30 AM
Last Post: Shruti7109162
  Adding a string value to a dictionary that is inside a list mahmoud899 1 377 Dec-15-2018, 02:31 PM
Last Post: ichabod801
  ValueError: Found input variables with inconsistent numbers of samples: [0, 3] ayaz786amd 2 3,066 Nov-27-2018, 07:12 AM
Last Post: ayaz786amd
  No Module of this name found wookie 4 2,748 Oct-31-2018, 07:17 PM
Last Post: wookie
  ImportError: DLL load failed: %1 is not a valid Win32 application. LiTing 1 3,086 Oct-14-2018, 09:52 PM
Last Post: snippsat
  Module not found error minifizikus 1 730 Sep-14-2018, 08:25 AM
Last Post: Axel_Erfurt
  Python installing as 32bit application on my 64bit system abdulganiyy 8 1,055 Jun-06-2018, 04:15 PM
Last Post: DeaD_EyE
  change application image issac_n 7 3,658 Feb-22-2018, 03:03 PM
Last Post: issac_n
  ImportError: DLL load failed: The specified procedure could not be found. chess 1 1,845 Sep-17-2017, 10:12 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)