0
0
Flaskframework~20 mins

Macros for reusable components in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Macro Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What does this Flask macro render?
Given the macro and its usage below, what HTML output will be rendered?
Flask
{% macro button(text, type='button') %}
  <button type="{{ type }}">{{ text }}</button>
{% endmacro %}

{{ button('Click me', 'submit') }}
A<button>Click me</button>
B<button type="submit">button</button>
C<button type="button">Click me</button>
D<button type="submit">Click me</button>
Attempts:
2 left
💡 Hint
Look at how the macro uses the parameters and what is passed when calling it.
📝 Syntax
intermediate
1:00remaining
Identify the syntax error in this macro definition
Which option shows the syntax error in the macro below?
Flask
{% macro list_items(items) %}
  <ul>
  {% for item in items %}
    <li>{{ item }}</li>
  {% endfor %}
  </ul>
{% endmacro %}
AIncorrect macro end tag: should be {% endmacro %} but is {% endmacro %}
BNo syntax error in the macro
CMissing closing tag for <ul>
DFor loop missing colon after {% for item in items %}
Attempts:
2 left
💡 Hint
Check Jinja2 macro and loop syntax carefully.
state_output
advanced
2:00remaining
What is the output of this macro call with nested macros?
Consider these macros and the call below. What HTML is produced?
Flask
{% macro badge(text) %}
  <span class="badge">{{ text }}</span>
{% endmacro %}

{% macro user_info(name, status) %}
  <div class="user">
    <h2>{{ name }}</h2>
    {{ badge(status) }}
  </div>
{% endmacro %}

{{ user_info('Alice', 'Active') }}
A
&lt;div class="user"&gt;
  &lt;h2&gt;Alice&lt;/h2&gt;
  &lt;span class="badge"&gt;Active&lt;/span&gt;
&lt;/div&gt;
B
&lt;div class="user"&gt;
  &lt;h2&gt;Alice&lt;/h2&gt;
  badge(Active)
&lt;/div&gt;
C
&lt;div class="user"&gt;
  &lt;h2&gt;Alice&lt;/h2&gt;
  &lt;span&gt;Active&lt;/span&gt;
&lt;/div&gt;
D
&lt;div class="user"&gt;
  &lt;h2&gt;Active&lt;/h2&gt;
  &lt;span class="badge"&gt;Alice&lt;/span&gt;
&lt;/div&gt;
Attempts:
2 left
💡 Hint
Look at how the user_info macro calls the badge macro inside it.
🔧 Debug
advanced
1:30remaining
Why does this macro raise an error?
This macro is called with None but raises an error. What is the cause?
Flask
{% macro first_item(items) %}
  {{ items[0] }}
{% endmacro %}

{{ first_item(None) }}
ATypeError because None is not subscriptable
BSyntaxError due to missing colon in macro
CIndexError because list is empty
DNo error, outputs None
Attempts:
2 left
💡 Hint
Think about what happens when you try to access index 0 of None.
🧠 Conceptual
expert
1:30remaining
Which statement about Flask macros is true?
Choose the correct statement about macros in Flask's Jinja2 templates.
AMacros can only be defined in Python files, not in templates.
BMacros automatically update the server-side Python variables when called.
CMacros can accept parameters and be reused with different arguments to generate HTML.
DMacros execute asynchronously to improve template rendering speed.
Attempts:
2 left
💡 Hint
Think about what macros do in templates.