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 trial

Python

I am following along with the Flat HTML video, using Python IDLE, and I have a bug

I am following along with the Flat HTML video, using Python IDLE, and I have a bug that I can't seem to figure out.

heres my simple_app.py script:

from flask import Flask
from flask import render_template

app = Flask(__name__)    

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


@app.route('/add/<int:num1>/<int:num2>')
@app.route('/add/<float:num1>/<float:num2>')
@app.route('/add/<float:num1>/<int:num2>')
@app.route('/add/<int:num1>/<float:num2>')
def add(num1, num2):
    return render_template("add.html")

runngingapp.run(debug=True, port=8100, host='0.0.0.0')   

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

Here is my add.html file and its location is /Users/KevinCleary/Documents/Python Scripts

<!doctype html> <html> <head> <title> Adding!</title> </head> <body> <h1>Num1 + Num2 = Sum-thing!</h1> </body> </html>

When I try to reload the web page, I get a completely blank page. No Errors or anything occur. I am very new to this, and I really would like to know whats going on. I'm not sure if theres a bug in my code, or the the templates folder is in the wrong place, or what.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Can you post the output of the console when you start the flask server and any output when you try to load the page http://0.0.0.0/add/1/2?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

What is the line runngingapp.run(debug=True, port=8100, host='0.0.0.0') for?

===== RESTART: /Users/KevinCleary/Documents/Python Scripts/simple_app.py =====
Traceback (most recent call last):
  File "/Users/KevinCleary/Documents/Python Scripts/simple_app.py", line 26, in <module>
    app.run(debug=True, port=8300, host='0.0.0.0')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/werkzeug/serving.py", line 675, in run_simple
    s.bind((hostname, port))
OSError: [Errno 48] Address already in use

I re-opened the the shell and my script, and when I re-ran it this is what it said back to me. 'scratching my head'

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

"already in use" means the port number is being used by another program. Make sure other instances of the flask server aren't running.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

I was able to get your code to work by:

  • createing a templates directory beneath the directory of simple_app.py
  • moved add.html to templates
  • commenting out runningapp line
  • restarting flask server

A) To answer your question, "What is the line runngingapp.run(debug=True, port=8100, host='0.0.0.0') for?" I'm not sure, it doesn't actually exist in the file, I must have made a mistake when copy and pasting it somehow. Apart from that one line, the original code matches the one posted.

B) In regards to the ' already in use' error. How do close the other running instance of flask? Also, how did you reboot the flask server?

C) Lastly, if you could elaborate on how you created the templates directory? I thought I had done that properly, but I guess not.

Thanks for all of the help. I know I am probably asking a lot of noob questions. Your answers are patient, insightful, and you have definitely helped me quite a bit. The tolerance is appreciated.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

How do close the other running instance of flask?

What OS and shell are you using?

If running in a bash shell, it's Ctrl-c. Or closing the shell window should work

Also, how did you reboot the flask server?

By restart, I meant to stop it (as above) and start it again using python simple_app.py

Lastly, if you could elaborate on how you created the templates directory?

At a prompt type:

mkdir templates

Or use a folder explorer to create it.

Noob question are welcomed!

Awesome! Everything now works like it is supposed to. Wow, this is addicting.