Python Flask Basics Templates and Static Files Flat HTML

Federico Lemaire
Federico Lemaire
8,422 Points

Problem with the code

I'm following Kenneth and I have the same code but when I reload the page I'm still having the first view, "Hello from Planet Earth" I don't know why it doesn't run the new view with the html code. Here is my code

from flask import Flask

app = Flask(name)

@app.route('/') @app.route('/<name>') def index(name='Planet Earth'): return 'Hello from {}'.format(name)

@app.route('/add/<int:num1>/<int:num2>') @app.route('/add/<float:num1>/<float:num2>') @app.route('/add/<int:num1>/<float:num2>') @app.route('/add/<float:num1>/<int:num2>') def add(num1, num2): return """ <!doctype html> <html> <head><title>Adding!</titel></head> <body> <h1>{} + {} = {}</h1> </body> </html> """.format(num1, num2, num1+num2)

app.run(debug=True, port=8000, host='0.0.0.0')

Federico Lemaire
Federico Lemaire
8,422 Points

MAybe I know where is the problem, it didn't appear the sum of num1 and num2 because are between the triple quotes, i'm using vs code, and change the color not as happens with kenneth

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Web Development Techdegree Student 35,917 Points

Hi Federico,

Please remember to put your code inside the correct markdown tags to prevent it being mangled when converted to HTML (e.g., look at what has happened to the line that should be app = Flask(__name__): it has been changed to app = Flask(name)). This is always important when asking a question about any language, but especially Python where whitespace is significant (unless you properly mark your code block, any quantity of whitespace gets converted to a single space) and in Python double underscores (dunders) are rampant (these get interpreted as bold text when not properly marked).

As for your question, what page are you loading? Are you loading /add/2/5 (the page Kenneth is loading) or just /. I reconstructed your code as best I could from the html and when I ran it I definitely got a different output from /add/2/5 than I get from just /.

However, I did get what looks like a blank page instead of the "2 + 5 = 7" that Kenneth gets. This is because you have a typo in your HTML. You open the <title> tag but then close a (nonexistent) <titel> so the browser thinks the whole page is inside your title tag. Once that typo is fixed, I get exactly the same output as Kenneth.

Hope that helps,

Cheers

Alex

Federico Lemaire
Federico Lemaire
8,422 Points

Alex, thanks a lot! I;ve got it! Sometimes it happen to me that I really pay attention and I see the exact code, and now I see that it is not the same as I wrote. So sorry and thank you for your time! I'll check it right know