I'm wanting a simple way to show rich HTML documentation of a python object in a Jupyter Notebook or JupyterLab context. It's possible this already exists. I basically want what Jupyter/IPython does when you call ?
on an object, except I want it as an inline HTML output.
The ability to generate a nice summary of a function or class's documentation exists in the builtin help
function. This text can be accessed directly with pydoc.render_doc
. When I looked for an HTML version of this, I found that all you have to do is specify an HTML renderer (shown below). Coupling this with the IPython rich display system (implementing _repr_html_()
) I made a simple class:
from pydoc import render_doc, HTMLDoc
class doc:
def __init__(self, obj):
self.obj = obj
def _repr_html_(self):
return render_doc(self.obj, renderer=HTMLDoc())
Which, when I run in the "Classic" Jupyter Notebook looks like this:
If I run the same in JupyterLab with dark theme I see this:
I don't like that it is right aligned, that it appears to be using the Notebook's warning color, and that some elements aren't visible in the dark theme of JupyterLab. Content also falls off the right margin and gets clipped, so the right alignment doesn't appear to work well. In the figure below I had to scroll the cell output to the right to see the overflowing text. 2/3 of the page is un-used to the left of that docstring, so it doesn't make any sense for it to overflow.
I think what I want is just the HTML output rendered as if it didn't inherit the notebook's CSS styles, so that it would look the same as if I opened the document in it's own tab. If these are shortcomings of pydoc, then that's understandable. It just seems like this should be quite doable with existing code from either pydoc, Jupyter, sphinx, or something.
I see things described in this question, but I don't know how to implement any of those answers within a _repr_html_()
method or within an IPython.display.HTML context. My CSS knowledge is rudimentary.