Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndy McDonald
Python Development Techdegree Graduate 13,801 PointsCant 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
Treehouse Moderator 68,427 PointsHey 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):
- http://192.168.0.114:8000
- http://192.168.0.114:8000/add/3/4 got back "You sexy MFer"
- http://192.168.0.114:8000/bob got back "Hello from bob"
$ 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
Python Development Techdegree Graduate 13,801 PointsAndy McDonald
Python Development Techdegree Graduate 13,801 PointsI 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
Treehouse Moderator 68,427 PointsChris Freeman
Treehouse Moderator 68,427 PointsIf 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.