-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
44 lines (34 loc) · 1.73 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import inspect
from IPython.display import Markdown
def show_doc(obj, name=None, title_level=3):
"""
Generates a Markdown description of a given class or function,
displaying its custom title or name, signature, and docstring in a formatted manner.
Parameters:
- obj: The class or function to document.
- name: Optional; A custom title for the documentation. If not provided, the object's name is used.
Returns:
- A Markdown representation of the class or function documentation.
"""
# Determine the anchor name based on the provided title or the object's name
anchor_name = name if name else obj.__name__
# Replace spaces with dashes and make lowercase for URL compatibility
anchor_id = anchor_name.replace(" ", "-").lower()
# Use the custom title if provided, otherwise fall back to the object's name
display_title = name if name else obj.__name__
# Get the signature, excluding the name from the signature for separate display
try:
signature = inspect.signature(obj)
except ValueError: # Handle cases where signature retrieval is not applicable
signature = ''
# Get the docstring
docstring = inspect.getdoc(obj)
if not docstring:
docstring = "No documentation available."
# Wrap the docstring in <pre> tags to preserve formatting
formatted_docstring = f"<pre>{docstring}</pre>"
title_marker = "#" * title_level
# Format the markdown output
markdown_output = f"{title_marker} <a id=\"{anchor_id}\">{display_title}</a>\n\n> {obj.__name__} `{signature}`\n\n{formatted_docstring}\n\n"
# Display the markdown
return Markdown(markdown_output)