Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython display always displays widgets before Text/Markdown

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?

like image 904
user5090779 Avatar asked Jul 25 '26 05:07

user5090779


1 Answers

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)
like image 173
James Draper Avatar answered Jul 27 '26 19:07

James Draper