You can use is_valid_xml
to determine if JSX is valid:
_valid = "<Foo></Foo>" # valid jsx
assert is_valid_xml(_valid)
If you pass invalid JSX to is_valid_xml
, a warning will be printed:
_invalid1 = "<Foo><Foo>"
_invalid2 = "<Foo></Bar>"
assert not is_valid_xml(_invalid1)
assert not is_valid_xml(_invalid2)
_fd = FunctionDoc(ex.function_with_types_in_docstring)
_p = _fd['Parameters'][0]
test_eq(param2JSX(_p), '<Parameter name="param1" type="int" desc="The first parameter. something something\\nsecond line. foo" />')
assert is_valid_xml(param2JSX(_p))
Below are some examples of docstrings and resulting JSX that comes out of them. This one is of a class:
print(inspect.getdoc(ex.ExampleClass))
_res = np2jsx(ex.ExampleClass)
assert '<Parameter name="attr1" type="str" desc="Description of `attr1`." />' in _res
assert 'extended_summary="If the class has public attributes' in _res
assert '</ParamSection>' in _res
print(_res)
This next one is of a top-level function:
print(inspect.getdoc(ex.function_with_types_in_docstring))
_res = np2jsx(ex.function_with_types_in_docstring)
assert 'extended_summary="`PEP 484`_ type annotations are supported' in _res
assert '<Parameter name="param2" type="str" desc="The second parameter." />' in _res
assert '<ParamSection name="Returns">' in _res
print(_res)
fmt_sig_param
converts individual parameters in signatures to JSX. Let's take the complex signature below, for example:
_sig = inspect.signature(ex.Bar)
_sig
Each of these parameters are then converted to JSX components
_ps = _sig.parameters
test_eq(fmt_sig_param(_ps['a']), '<SigArg name="a" type="int" />')
test_eq(fmt_sig_param(_ps['b']), '<SigArg name="b" type="str" default="foo" />')
test_eq(fmt_sig_param(_ps['args']), '<SigArg name="*args" />')
test_eq(fmt_sig_param(_ps['tags']), '<SigArg name="**tags" />')
assert is_valid_xml(fmt_sig_param(_ps['b']))
Let's take the class Bar, for example:
inspect.signature(ex.Bar)
The signature will get converted to JSX components, like so:
_ex_result="""<SigArgSection>
<SigArg name="a" type="int" /><SigArg name="b" type="str" default="foo" /><SigArg name="c" type="float" default="0.1" /><SigArg name="*args" /><SigArg name="**tags" />
</SigArgSection>
""".strip()
_gen_result = get_sig_section(ex.Bar)
assert is_valid_xml(_gen_result) # make sure its valid xml
test_eq(_gen_result, _ex_result)
print(_gen_result)
test_eq(get_type(ex.function_with_types_in_docstring),'function')
test_eq(get_type(ex.ExampleClass), 'class')
test_eq(get_type(ex.ExampleClass.example_method), 'method')
This project has a settings.ini which defines the module_baseurls
parameter. get_base_urls
will return a dictionary of all the baseurls defined there. This is useful used to construct URLs to source code in documentation.
Here is how the relevant parts of this project's settings.ini
file is defined:
module_baseurls = metaflow=https://github.com/Netflix/metaflow/tree/master/
nbdev=https://github.com/fastai/nbdev/tree/master
fastcore=https://github.com/fastcore/tree/master
_base_urls = get_base_urls()
assert len(_base_urls.keys()) == 3
_base_urls
ShowDoc
will render the function signature as well as other info that may help you author documentation for a function, method, or class in a notebook:
Below, we render the docs for a class:
ShowDoc(ex.ExampleClass)
There is a special override for decorators:
ShowDoc(ex.ExampleClass, decorator=True, name='example')
There is also a special override for the module name:
ShowDoc(ex.ExampleClass, decorator=True, name='example', module_nm='mymodule.foo')
ShowDoc(ex.ExampleClass, skip_sections='Attributes')
Below, we render docs for method. Note that you will receive an warning if your docstrings are not able to be parsed according to the numpy format:
ShowDoc(ex.ExampleClass.example_method)
We can also render properties as well:
ShowDoc(ex.ExampleClass.readonly_property)
Finally, we can also show docs for a function. If the module associated with the object has a baseurl specified in your project's settings.ini
file as described in get_base_urls
, you will also see a link to the source code:
ShowDoc(test_eq)
As a debugging tool, ShowDoc.jsx
extracts JSX Markup about an object so that you can use it for code documentation. Here are some examples:
_result = ShowDoc(ex.ExampleClass).jsx
assert is_valid_xml(_result)
print(_result)
_result = ShowDoc(ex.Foo).jsx
assert is_valid_xml(_result)
assert 'link' not in _result
print(_result)