Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Templates and Static Files Flat HTML

Andy McDonald
Andy McDonald
4,988 Points

Cant get second function to show

Trying to follow along in this video but when i do the add function and I run the website I don't see anything that has to do with the add function. I only get 'index' function to work.

Furthermore... It seemed like this was broken for me after a couple tries. I would change characters in the index function, even deleted it entirely and I would still get "Hello from Treehouse".

Don't know what to do here. Is it this HTML function that's not working or does it have something to do with my code?

Heres what I have:

from flask import Flask

app = Flask(__name__)

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


@app.route('/add/<int:num1>/<int:num2>')
@app.route('/add/<int:num1>/<float:num2>')
@app.route('/add/<float:num1>/<int:num2>')
@app.route('/add/<float:num1>/<float:num2>')
def add(num1, num2):
    return """
    <!doctype>
    <html>
        <head><title>Hello MFer</title></head>
        <body>
        <h1>You sexy MFer</h1>
        </body>
    </html>
    """.format(num1, num2, num1+num2)

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

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,464 Points

Hey Andy McDonald, running your code seems ok. Saved to a file "flask1.py", Then in my browser, I requested the URLS (note, your IP addresses may differ):

$ python flask1.py 
 * Serving Flask app 'flask1' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://192.168.0.114:8000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 915-892-228
192.168.0.114 - - [13/Jul/2021 22:48:02] "GET / HTTP/1.1" 200 -
192.168.0.114 - - [13/Jul/2021 22:48:02] "GET /favicon.ico HTTP/1.1" 200 -
192.168.0.114 - - [13/Jul/2021 22:48:10] "GET /add/3/4 HTTP/1.1" 200 -
192.168.0.114 - - [13/Jul/2021 22:48:57] "GET /bob HTTP/1.1" 200 -

What URLs are you using after starting the app?

Andy McDonald
Andy McDonald
4,988 Points

I guess i didnt fully understand how the url was interacting with the script. I expected it to show me You sexy MFer just by adding /add at the end but i guess i need to plug in the next to arguments for that to work.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 Points

If you did want to have an “/add” route, you would also need to add default values for num1 and num2 in the function definition. Otherwise, the “/add” route would cause num1 to be undefined.