NewbieQ: Jupyter HTML command not working when multiple code lines
<p>I'm new to Jupyter Notebook and have this simple line of code:</p>

<pre><code>HTML('&lt;b&gt;Question&lt;/b&gt;')
Q=1
</code></pre>

<p>I have found that the first line only works when nothing else comes after it. Is this normal? Do I have to have each line of code in a separate cell?</p>

<p>There are two things at play here:</p>

<ol>
<li><code>HTML</code> creates an HTML <em>object</em>, whose <a href="https://nbviewer.jupyter.org/github/ipython/ipython/blob/5.0.0/examples/IPython%20Kernel/Rich%20Output.ipynb" rel="nofollow">rich representation</a> is the HTML string <code>&lt;b&gt;...</code>. Instantiating such an object does not display it.</li>
<li>If a cell ends in an expression (the cell has a result, in a way), the result of that expression is displayed automatically.</li>
</ol>

<p>So a cell that <em>ends with</em> <code>HTML(...)</code> will display that HTML because it is the 'result' of the cell. If you store the HTML object in a variable, you can display it at any time. IPython has a <code>display</code> function, which is like the Jupyter rich-media version of Python's builtin <code>print</code> function. To display objects at any point:</p>

<pre><code>from IPython.display import display
display(obj)
</code></pre>

<p>in your case:</p>

<pre><code>display(HTML('&lt;b&gt;Question&lt;/b&gt;'))
</code></pre>

<p>Or you can store the HTML in a variable and display it later or repeatedly:</p>

<pre><code>msg = HTML('&lt;b&gt;Question&lt;/b&gt;')
for i in range(3):
    display(msg)
</code></pre>

python jupyter