Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom attributes to HTML templates #335

Open
silverbucket opened this issue Apr 14, 2021 · 4 comments · May be fixed by #336
Open

Custom attributes to HTML templates #335

silverbucket opened this issue Apr 14, 2021 · 4 comments · May be fixed by #336

Comments

@silverbucket
Copy link

I would like to append the documentation with auxiliary data which would be collected at the time of document generation. (eg. test results).

Expected Behavior

When using pdoc3 programmatically, it would be nice if additional variables could be attached to the Class/Method objects, which could be accessed from within the templates.

Actual Behavior

Currently I'm unable to find any way to add custom variables.

Additional info

  • pdoc version: 0.9.2
@kernc
Copy link
Member

kernc commented Apr 14, 2021

Can you show a full example how you'd use it?

pdoc.Class.obj points to the underlying type object. Module and Function likewise.

@silverbucket
Copy link
Author

silverbucket commented Apr 15, 2021

Thanks for that tip, (re: obj) it's what I needed to make further progress. What I'm currently doing is collecting pytest results and including them along-side the test suite documentation.

 # test_results contains dict of test results collected from pytest junit xml
context = pdoc.Context()
module = pdoc.Module('test/my_tests.py')
pdoc.link_inheritance(context)

for c in module.classes():
    for m in c.methods():
        m.obj.test_results = {}
        if m.refname in test_results:
            m.obj.test_results = test_results[m.refname]

print(module.html())

This is working fine for me now. I wonder if this is the supported approach, and if so perhaps an example could be added to the documentation (I'm willing to submit a PR if so).

Additionally, now that I'm using the programatic approach, filtering was no longer working. In the test suite I have a number of classes and methods set in __pdoc__ to be ignored. This worked fine when I used the command-line tool, I eventually got filtering working, though some warnings are being thrown and I'm not sure my approach is the best.

    context = pdoc.Context()
    module = pdoc.Module('test/my_tests.py')
    pdoc.link_inheritance(context)

    def filter_objects(obj):
        if obj.refname.replace('my_tests.', '') in module.__pdoc__:
            return False
        return True

    for c in module.classes():
        for m in c.methods():
            m.obj.test_results = {}
            if m.refname in test_results:
                m.obj.test_results = test_results[m.refname]

    print(pdoc.html(module, docfilter=filter_objects))

This works, however I get the warnings:

.../venv/lib/python3.8/site-packages/pdoc/__init__.py:782: UserWarning: __pdoc__-overriden key 'client' does not exist in module 'my_tests'
  warn('__pdoc__-overriden key {!r} does not exist '
.../venv/lib/python3.8/site-packages/pdoc/__init__.py:782: UserWarning: __pdoc__-overriden key 'BasicTest' does not exist in module 'my_tests
  warn('__pdoc__-overriden key {!r} does not exist '

Both client and BasicTest are two items that are set to be ignored.

While currently I'm just working with one file, I wonder how well this implementation will scale as more test files are added. Any input welcome, and I'd be happy to help improve the documentation on this procedure once I'm confident in the approach.

@kernc
Copy link
Member

kernc commented Apr 16, 2021

You need to pass the context instance to modules you create:

module = pdoc.Module(..., context=context)

Other than that, I don't see why module's __pdoc__ would be ignored.

@silverbucket
Copy link
Author

@kernc Ah, of course - I had this originally added but must have removed it without noticing during experimentation. Re-adding context solves the issue of filtering. I've created a small PR mentioning the availability of the obj property, for your consideration in merging. (Would have saved me some time in getting started with custom templates).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants