Bird
Raised Fist0
Flaskframework~20 mins

Flask project structure conventions - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flask Structure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you run this Flask app with this structure?

Consider a Flask app with this structure:

myapp/
  app.py
  templates/
    home.html
  static/
    style.css

The app.py contains:

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

What will the app do when you visit the root URL?

Flask
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')
AServe the style.css file automatically at the root URL.
BRaise a TemplateNotFound error because templates folder is missing.
CRender the home.html template from the templates folder successfully.
DReturn a 404 error because no route is defined.
Attempts:
2 left
💡 Hint

Flask looks for templates in the templates folder by default.

📝 Syntax
intermediate
1:00remaining
Which folder is Flask expecting for static files by default?

In a standard Flask project, where should you place CSS, JavaScript, and image files so Flask can serve them automatically?

Aassets
Bstatic
Cpublic
Dresources
Attempts:
2 left
💡 Hint

Flask uses a default folder name for static files.

🔧 Debug
advanced
2:30remaining
Why does this Flask app fail to find the template?

Given this project structure:

myapp/
  app/
    __init__.py
    routes.py
  templates/
    index.html

And app/__init__.py contains:

from flask import Flask
app = Flask(__name__)

from app import routes

But running the app raises TemplateNotFound when rendering index.html. What is the likely cause?

Flask
from flask import Flask
app = Flask(__name__)

from app import routes
AThe __init__.py file is missing a route decorator.
BThe templates folder must be renamed to 'views' for Flask to find it.
CFlask cannot find templates because the app variable is not named 'application'.
DThe Flask app is created in a subpackage, so the templates folder should be inside the app folder.
Attempts:
2 left
💡 Hint

Flask looks for templates relative to the app package location.

🧠 Conceptual
advanced
1:30remaining
What is the purpose of the __init__.py file in a Flask app package?

In a Flask project structured as a package, why do we include an __init__.py file inside the app folder?

AIt marks the folder as a Python package and initializes the Flask app instance.
BIt contains all the HTML templates for the app.
CIt stores static files like CSS and JavaScript.
DIt is required to run the Flask development server.
Attempts:
2 left
💡 Hint

Think about Python packages and app initialization.

❓ state_output
expert
3:00remaining
How many routes are registered in this Flask app structure?

Given this Flask project structure:

myapp/
  app/
    __init__.py
    routes.py
  run.py

With app/__init__.py:

from flask import Flask
app = Flask(__name__)

from app import routes

And app/routes.py:

from app import app

@app.route('/')
def home():
    return 'Home'

@app.route('/about')
def about():
    return 'About'

And run.py:

from app import app

if __name__ == '__main__':
    app.run()

How many routes will the app have when running?

Flask
from flask import Flask
app = Flask(__name__)

from app import routes

# routes.py
from app import app

@app.route('/')
def home():
    return 'Home'

@app.route('/about')
def about():
    return 'About'

# run.py
from app import app

if __name__ == '__main__':
    app.run()
A2 routes: '/' and '/about'
B3 routes including a default error handler
CNo routes registered because routes.py is not imported
D1 route: '/' only
Attempts:
2 left
💡 Hint

Check where routes are defined and imported.

Practice

(1/5)
1. Which folder is conventionally used to store HTML files in a Flask project?
easy
A. templates
B. static
C. app
D. config

Solution

  1. Step 1: Understand Flask folder roles

    Flask uses a folder named templates to hold HTML files for rendering views.
  2. Step 2: Identify correct folder for HTML

    Since HTML files are templates, they belong in the templates folder, not static or others.
  3. Final Answer:

    templates -> Option A
  4. Quick Check:

    HTML files go in templates [OK]
Hint: HTML files live in templates folder by Flask convention [OK]
Common Mistakes:
  • Confusing static folder for HTML files
  • Putting HTML files directly in app folder
  • Using config folder for templates
2. Which file is used to initialize a Flask app package inside the app folder?
easy
A. run.py
B. config.py
C. __init__.py
D. app.py

Solution

  1. Step 1: Recognize Python package initialization

    Python packages use __init__.py to initialize modules and setup code.
  2. Step 2: Apply to Flask app folder

    In Flask, the app folder is a package, so __init__.py initializes the Flask app instance.
  3. Final Answer:

    __init__.py -> Option C
  4. Quick Check:

    __init__.py initializes app package [OK]
Hint: Use __init__.py to initialize app package in Flask [OK]
Common Mistakes:
  • Using run.py to initialize app package
  • Confusing config.py with initialization
  • Naming app.py as initializer
3. Given this Flask project structure:
myproject/
 ├── app/
 │    ├── __init__.py
 │    ├── routes.py
 │    ├── templates/
 │    │    └── home.html
 │    └── static/
 │         └── style.css
 ├── config.py
 └── run.py

Which file is typically used to start the Flask application?
medium
A. config.py
B. app/__init__.py
C. app/routes.py
D. run.py

Solution

  1. Step 1: Identify the purpose of run.py

    In Flask projects, run.py is commonly used as the entry point to start the app server.
  2. Step 2: Differentiate from other files

    config.py holds settings, __init__.py initializes app, and routes.py defines routes, but none start the server directly.
  3. Final Answer:

    run.py -> Option D
  4. Quick Check:

    run.py starts Flask app [OK]
Hint: run.py usually starts the Flask app server [OK]
Common Mistakes:
  • Trying to run config.py to start app
  • Running __init__.py directly
  • Running routes.py as main file
4. You have this run.py file:
from app import app

if __name__ == '__main__':
    app.run(debug=true)

But running python run.py gives an ImportError: cannot import name 'app'. What is the likely cause?
medium
A. run.py is missing the Flask import statement
B. The 'app' variable is not defined in app/__init__.py
C. The templates folder is missing
D. Debug mode cannot be set in run.py

Solution

  1. Step 1: Understand the import error

    The error means app is not found inside the app package's __init__.py.
  2. Step 2: Check app/__init__.py for app variable

    Usually, app = Flask(__name__) is defined in __init__.py. If missing, import fails.
  3. Final Answer:

    The 'app' variable is not defined in app/__init__.py -> Option B
  4. Quick Check:

    app must be defined in __init__.py [OK]
Hint: Ensure app variable is defined in __init__.py before importing [OK]
Common Mistakes:
  • Assuming missing Flask import causes this error
  • Thinking templates folder affects imports
  • Believing debug mode setting causes import error
5. You want to organize a Flask project with multiple blueprints for different features. Which structure best supports this while following Flask conventions?
hard
A.
myproject/
 ├── app/
 │    ├── __init__.py
 │    ├── auth/
 │    │    ├── __init__.py
 │    │    └── routes.py
 │    ├── blog/
 │    │    ├── __init__.py
 │    │    └── routes.py
 │    ├── templates/
 │    └── static/
 ├── config.py
 └── run.py
B.
myproject/
 ├── auth/
 │    ├── __init__.py
 │    └── routes.py
 ├── blog/
 │    ├── __init__.py
 │    └── routes.py
 ├── templates/
 ├── static/
 ├── config.py
 └── run.py
C.
myproject/
 ├── app.py
 ├── auth_routes.py
 ├── blog_routes.py
 ├── templates/
 ├── static/
 ├── config.py
 └── run.py
D.
myproject/
 ├── app/
 │    ├── routes.py
 │    ├── templates/
 │    └── static/
 ├── auth.py
 ├── blog.py
 ├── config.py
 └── run.py

Solution

  1. Step 1: Understand blueprint organization

    Blueprints are best organized as sub-packages inside the main app package, each with its own __init__.py and routes.
  2. Step 2: Evaluate options for Flask conventions

    myproject/
     ├── app/
     │    ├── __init__.py
     │    ├── auth/
     │    │    ├── __init__.py
     │    │    └── routes.py
     │    ├── blog/
     │    │    ├── __init__.py
     │    │    └── routes.py
     │    ├── templates/
     │    └── static/
     ├── config.py
     └── run.py
    places blueprints inside app/ folder as sub-packages, keeping templates and static centralized, which is recommended.
  3. Final Answer:

    Option A structure best supports multiple blueprints with Flask conventions -> Option A
  4. Quick Check:

    Blueprints as sub-packages inside app folder [OK]
Hint: Put blueprints as subfolders inside app with __init__.py [OK]
Common Mistakes:
  • Placing blueprints outside app folder
  • Using single file for all routes
  • Not including __init__.py in blueprint folders
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard