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 Flask Basics Welcome to Flask First Steps

Chen Wang
Chen Wang
7,371 Points

how can I view the .py file in a browser?

I mean if I write the code locally, it seems I cannot drag it directly to a browser. If I do so, I can just see the scripts in browser.

2 Answers

Jose Soto
Jose Soto
23,407 Points

Python does not output to the browser. That is what Flask will help you do. Essentially, you need to output your python script via a Flask view/controller. You'll get more information on how to do that in later videos.

Change IP address in the script to your local IP which is 127.0.0.1

app.run( port=80, host='127.0.0.1')

Open your browser and go to 127.0.0.1 or localhost and you can see your 'Hello World!' now

If you change route to something like

@app.route('/mynewroute')

you can access it via 127.0.0.1/mynewroute or localhost/mynewroute

If you are using XAMPP or any other program which uses port 80, you need to change the port number to something like 8000 in your script

app.run( port=8000, host='127.0.0.1')

Now you can access your page in browser by going to localhost:8000 or 127.0.0.1:8000