0
votes

I have python code I would like a html page to display for:

I get errors return render_template('index.html')

I have tried specifying the template folder but it doesnt work also.

my project directory:

/api
-- api.py
-- /templates
---- index.html

i am also running the server like: flask run -h 192.168.x.x -p 8080

jinja2.exceptions.TemplateNotFound: index.html
- - [29/Feb/2020 20:35:20] "GET / HTTP/1.1" 500 -


   from flask import Flask, render_template
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
app = Flask("__main__")
pwm = GPIO.PWM(3, 50)
pwm.start(0)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/switch/<val>')
def switch(val):
    if val == "on":
        SetAngle(0)
    elif val == "off":
        SetAngle(180)
    return

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')


def SetAngle(angle):
    duty = angle / 18 + 2
    GPIO.output(3, True)
    pwm.ChangeDutyCycle(duty)
    sleep(1)
    GPIO.output(3, False)
    pwm.ChangeDutyCycle(0)
2
Set app.config['EXPLAIN_TEMPLATE_LOADING'] = True to check where flask is looking for your templates. What does it say? - mrzo

2 Answers

0
votes

Did you specify the file using '/templates/index.py'? If so python will think the templates directory is at the root of your file system '/', Instead write 'templates/index.py'.

If you are working on Windows, you may have to replace / with \, and in that case you should write 'templates\index.py'.

0
votes

From what I can see from your code, everything should be working! But if it's not, the plausible answers are that you could be using a corrupted/tampered version of flask or you must have mispelt something. Hope this clears the issue!