Flask Issues - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Flask Issues (/thread-674.html) Pages:
1
2
|
Flask Issues - iainstott - Oct-27-2016 Hi guys, hoping someone fancies helping me out. I have written a central heating control script in python 2, and as I haven't really done much with python 3 yet and I have some new features I want to add, I have decided to re-write the script using python 3 and a bit better planning. I started some today and thought I would get flask up and running within the script before going on to other parts, I have flask working to a degree using a link I can't post yet. I got it to return hello world in the first part, but then when moving over to templates I get server 500. Then I always get this when i run the script My folder structure/ /run.py /app /app/templates /app/templates/index.html /app/__init__.py /app/views.py run.py #v0.0.1 #!/bin/python3 import os, sys, subprocess, socket, sqlite3 import math, glob import datetime, time import RPi.GPIO as GPIO from app import app workingdir='/media/Backup/GitHub/Heating' lib_path = os.path.abspath(os.path.join(workingdir, 'lib')) sys.path.append(lib_path) day_temp=19.999 night_temp=23.999 daytime=['00:00' '17:00'] detectIP=['192.168.0.25', '192.168.0.26'] onCommand="ssh osmc@192.168.0.130 'sudo gpio write 8 0'" offCommand="ssh osmc@192.168.0.130 'sudo gpio write 8 1'" templog='app/database/templog_' datalog='app/database/datalog.db' status='app/variables/status' manual='app/variables/manual' wintermode='app/variables/winter' advance='app/variables/advance' GPIO.setmode(GPIO.BCM) GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) hostname=socket.gethostname() now = datetime.datetime.now() time_now='' date='' app.run('0.0.0.0', '8080')app/views.py #!flask/bin/python3 from app import app @app.route('/') @app.route('/index') def home(): return render_template("index.html") @app.route('/advance', methods=['GET', 'POST']) def advance(): return redirect("/") @app.route('/manual', methods=['GET', 'POST']) def manual(): return redirect("/") @app.route('/winter', methods=['GET', 'POST']) def winter(): return redirect("/")app/__init__.py #!flask/bin/python3 from flask import Flask, render_template, request, redirect app = Flask(__name__) from app import viewsCheers Iain RE: Flask Issues - micseydel - Oct-27-2016 I haven't tested this, but I suspect app.run('0.0.0.0', '8080')should be app.run('0.0.0.0', 8080) RE: Flask Issues - iainstott - Oct-27-2016 (Oct-27-2016, 06:58 PM)micseydel Wrote: I haven't tested this, but I suspect Thank you youre a diamond,that has gotten ridden of the error message on script run. I still am experiencing 500 error when using a template RE: Flask Issues - micseydel - Oct-27-2016 You'll need to provide more details, preferably including a traceback like above. RE: Flask Issues - iainstott - Oct-27-2016 (Oct-27-2016, 07:26 PM)micseydel Wrote: You'll need to provide more details, preferably including a traceback like above. Thank you, you provoked thought for me to solve it. After turning on debug on, loading the webpage gave me render_template not defined. Quick look back i hadn't imported it in to views.py RE: Flask Issues - iainstott - Oct-28-2016 Ok so new problem I cant seem to get right. I am trying to define some images in the css file but they wont work. #submit:hover { background: url("{{ url_for('static', filename='images/blueButton.png') }}") 0 0 no-repeat; }the script out put gives me And I cant seem to find anywhere the correct syntax for the css.Cheers Iain RE: Flask Issues - iainstott - Oct-28-2016 Ok so i have the images working by adding the css for the buttons to the html file, but I still cant find the way to do it to the actual css file where the images will work. Is it possible that way. Cheers Iain RE: Flask Issues - snippsat - Oct-28-2016 You need a static folder where you place images. A demo. Folder: img_demo\ app.py templates\ index.html static\ images\ back.jpgapp.py: from flask import Flask, render_template app = Flask(__name__, static_url_path='') @app.route("/") def bak_image(): return render_template("index.html") if __name__ == '__main__': app.run()index.html: <html> <body> <p>Does the Image Work?</p> <style type="text/css"> body { background: url("{{ url_for('static', filename='images/back.jpg') }}") 0 0 no-repeat; } </style> </body> </html>So this will load "back.jpg" as background image. Now i have CSS in html file. For bigger stuff always have CSS in separate file, it in also places in static folder. Eg. static\ css\ style.ccs js\ some_javascript_stuff.js RE: Flask Issues - iainstott - Oct-28-2016 (Oct-28-2016, 02:52 PM)snippsat Wrote: You need a static folder where you place images. Thank you for the reply, I have the static folder setup and index.html is reading the my css template. But when trying to define the button image in the css template like #submit { color: #fff; font-size: 0; width: 135px; height: 60px; border: none; margin: 0; padding: 0; background: url("static/images/blueButton.png") 0 0 no-repeat; } #submit:hover { background: url("{{ url_for('static', filename='images/blueButton.png') }}") 0 0 no-repeat; }gives me. Where as if I add it to the index.html in style tags it works fine.With the %7B%7B%20 stuff being in the error, I'm thinking its got something to do with whitespaces or special characters, so is my syntax off?? submit:hover is the right link im trying above, the submit one I know is wrong it is just me playing trying to get it to work RE: Flask Issues - snippsat - Oct-28-2016 In a css file "url_for" will not work,and will give error as you get. Can give relative path to image in css file. If a make a "style.css" in my demo code with this in it. body { background-image: url('../images/back.jpg'); background-repeat-x: no-repeat; background-repeat-y: no-repeat; }In index.html i call "style.css" like this in head. <head> <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" /> </head> |