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:
can you guys come to the rescue again?
paul
EDIT following request:
Homeheating.models.Pins:
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

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/.