diff --git a/doc/tutorials/html_runner.py b/doc/tutorials/html_runner.py
index f067d35e0ca..cbaf3b95403 100644
--- a/doc/tutorials/html_runner.py
+++ b/doc/tutorials/html_runner.py
@@ -196,15 +196,16 @@ def execute_notebook(nb, src, cell_separator):
for filepath in args.scripts:
add_cell_from_script(nb, filepath)
+# convert solution cells to code cells
+if args.exercise2:
+ convert_exercise2_to_code(nb)
+
# disable plot interactivity
disable_plot_interactivity(nb)
# guard against a jupyter bug involving matplotlib
split_matplotlib_cells(nb)
-if args.exercise2:
- convert_exercise2_to_code(nb)
-
if args.substitutions or args.execute:
# substitute global variables
cell_separator = '\n##{}\n'.format(uuid.uuid4().hex)
diff --git a/testsuite/scripts/tutorials/test_html_runner.py b/testsuite/scripts/tutorials/test_html_runner.py
index 89b625046cb..8d8221675e9 100644
--- a/testsuite/scripts/tutorials/test_html_runner.py
+++ b/testsuite/scripts/tutorials/test_html_runner.py
@@ -157,7 +157,7 @@ def test_exercise2_plugin(self):
cell_md['metadata']['solution2_first'] = True
cell_md['metadata']['solution2'] = 'hidden'
nb['cells'].append(cell_md)
- code = '```python\n2\nglobal_var = 5\n```'
+ code = '```python\n2\nimport matplotlib.pyplot\nglobal_var = 5\n```'
cell_md = nbformat.v4.new_markdown_cell(source=code)
cell_md['metadata']['solution2'] = 'hidden'
nb['cells'].append(cell_md)
@@ -195,7 +195,10 @@ def test_exercise2_plugin(self):
self.assertEqual(cell['source'], 'Question 2')
cell = next(cells)
self.assertEqual(cell['cell_type'], 'code')
- self.assertEqual(cell['source'], '2\nglobal_var = 20')
+ self.assertEqual(cell['source'], '2\nimport matplotlib.pyplot')
+ cell = next(cells)
+ self.assertEqual(cell['cell_type'], 'code')
+ self.assertEqual(cell['source'], 'global_var = 20')
cell = next(cells)
self.assertEqual(cell['cell_type'], 'code')
self.assertEqual(cell['source'], '3')