0
0
Flaskframework~20 mins

Flask project structure conventions - Practice Problems & Coding Challenges

Choose your learning style9 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.