Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

python - Displaying dictionary data in Sphinx documentation

I have a dictionary in Python project source code which describes default configuration values. The dictionary is quite lengthy. I'd like to see dictionary in Sphinx documentation in other format besides "View source", so that people can quickly check for the default values.

Does Sphinx provide options to format dictionary-like variables for human-readable format when used with Sphinx autodoc? I am currently using .. automodule:: to dump out the whole module and I get the dictionary as one long string dump in the documentation (no newlines, pretty printing, anything), being basically unreadable.

  • Does Sphinx provide tools to print out the value of individual source code variables

  • Is there any pretty printing available?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This may not be the most elegant solution (it would be much better to write a proper directive to output a pretty-printed dictionary), but this works for now:

Add the custom exec directive given here to your Sphinx .conf file, then, in the .rst file you want to print the dictionary, do something like this:

.. exec::
    import json
    from some_module import some_dictionary
    json_obj = json.dumps(some_dictionary, sort_keys=True, indent=4)
    print '.. code-block:: JavaScript

    %s

' % json_obj

That will print out your dictionary in a JavaScript code block in your docs (which I find to be the best way to render dictionaries in the docs).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...