2
votes

I have been learning flask/ python from this tutorial http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

This blog is pretty good and it explain very well. In the first tutorial, if you notice, he asks us to create a init.py file, a views.py file and a main run.py file.

root
/microblog
  /apps
    /__init__.py
    views.py

I created the two files. He asks us to create a run.py file and put it in the root file. then

chmod a+x run.py
./run.py

It says the file does not exist. If I,

python run.py

It says App module not defined. I cannot figure out the problem, I put the run.py file in all the files, it doesnt not work what so ever.

I will also include the code so that it would be easier to answer instead of going to the above link

init.py

from flask import Flask
app = Flask(__name__)
from app import views

views.py

from app import app
@app.route('/')
@app.route('/index')
def index():
  return 'Hello world'

run.py

#!flask/bin/python
from app import app
app.run(debug = True)

My questions:

  1. Where should I put the run.py file?

  2. Why are we creating different files? Why cant all of them be in one full file?

    init.py -->

    • he is importing flask which is normal. then assigning app = (name). why is that? Then why is he importing views from apps?

views.py -->

  1. from app import app? is app an existing module here or the one we just created? what does @app.route('/') or
    @app.route('/index')do?

Can some one put the code together and explain it?

1
Please bare the long questions. Help me here.druuu

1 Answers

2
votes

It says App module not defined

You misspelled package name: you have apps in your directory tree and you try to import app

Where should I put the run.py file?

Anywhere you want as long app is in PYTHONPATH. Or you can put it inside microblog directory.

he is importing flask which is normal. then assigning app = (name). why is that?

# Create reference to flask WSGI application object
app = Flask(__name__)

Why? Because you need application to run. See official docs: Flask object

Then why is he importing views from apps?

from app import views

means: From package named app import module named views

Naming convention could be probably different but if you don't see the difference you should probably spend more learning python basics before starting with more sophisticated stuff.

from app import app? is app an existing module here or the one we just created? what > does @app.route('/') or @app.route('/index')do?

@app.route('/')
def index():
    return 'Hello world'

Short answer: if app receives request for url '/' responds using function foo For more see official docs: add_url_rule and URL Route Registrations

Update

Why are we creating different files? Why cant all of them be in one full file?

Actually nothing stops you from putting everything inside one file but most of the time it is really bad idea. If you ask about reasoning in this specific example it is mostly about separating modules that have different responsibilities.