I am working on a project on the Jupyter Notebook using the IPython module, and I am trying to display widgets and Markdown-formatted text at the same time. However, I am unable to display text before displaying the widgets. The following code snippet, for example:
import ipywidgets as widgets
from IPython.display import display, Markdown
display(Markdown("## Enter your name"))
name = widgets.Text(description="Enter your name: ")
display(name)
displays this output, even though I asked to display the markdown before displaying the widget. How can I force Jupyter Notebook to display in the order I want?
I believe that this bug has been fixed in the latest version 7 of ipywidgets. Try the same code after updating to 7.0. You can upgrade with following command (assuming you are running on anaconda).
conda install -c conda-forge ipywidgets
If it still doesn't work for you try using dominate and the HTML widget. First install dominate from the command line, pip install dominate then you can run the following;
import ipywidgets as widgets
from dominate import tags
from IPython.display import display
header = widgets.HTML(tags.h2("Enter your name").render())
name = widgets.Text(description="Enter your name: ")
display(header, name)
For an improved layout here is the same code from above with the HBox and Label widgets;
import ipywidgets as widgets
from dominate import tags
from IPython.display import display
header = widgets.HTML(tags.h3("Enter your name").render())
name = widgets.Text()
namebox = widgets.HBox([widgets.Label("Enter your name: "), name])
display(header, namebox)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With