Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask Issues
#1
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
Output:
--- Logging error --- Traceback (most recent call last):   File "/usr/lib/python3.4/logging/__init__.py", line 978, in emit     msg = self.format(record)   File "/usr/lib/python3.4/logging/__init__.py", line 828, in format     return fmt.format(record)   File "/usr/lib/python3.4/logging/__init__.py", line 565, in format     record.message = record.getMessage()   File "/usr/lib/python3.4/logging/__init__.py", line 328, in getMessage     msg = msg % self.args TypeError: %d format: a number is required, not str Call stack:   File "run.py", line 37, in <module>     app.run('0.0.0.0', '8080')   File "/usr/lib/python3/dist-packages/flask/app.py", line 772, in run     run_simple(host, port, self, **options)   File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 699, in run_simple     and 'http' or 'https', display_hostname, port)   File "/usr/lib/python3/dist-packages/werkzeug/_internal.py", line 87, in _log     getattr(_logger, type)(message.rstrip(), *args, **kwargs) Message: ' * Running on %s://%s:%d/' Arguments: ('http', '0.0.0.0', '8080')
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 [email protected] 'sudo gpio write 8 0'"
offCommand="ssh [email protected] '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 views
Cheers Iain
Reply
#2
I haven't tested this, but I suspect
app.run('0.0.0.0', '8080')
should be
app.run('0.0.0.0', 8080)
Reply
#3
(Oct-27-2016, 06:58 PM)micseydel Wrote: I haven't tested this, but I suspect
app.run('0.0.0.0', '8080')
should be
app.run('0.0.0.0', 8080)

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
Reply
#4
You'll need to provide more details, preferably including a traceback like above.
Reply
#5
(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
Reply
#6
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
Error:
GET /static/styles/%7B%7B%20url_for('static',%20filename='images/blueButton.png')%20%7D%7D HTTP/1.1" 404 -
And I cant seem to find anywhere the correct syntax for the css.

Cheers
Iain
Reply
#7
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
Reply
#8
You need a static folder where you place images.
A  demo.
Folder:
img_demo\
app.py
    templates\
    index.html
   static\
       images\
       back.jpg
app.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
Reply
#9
(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.
Error:
[color=#000000][size=x-small][font=monospace]GET /static/styles/%7B%7B%20url_for('static',%20filename='images/blueButton.png')%20%7D%7D HTTP/1.1" 404 -[/font][/size][/color]
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
Reply
#10
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>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  flask issues with app structuring sidk23396 1 1,702 Feb-12-2020, 11:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020