Search code examples
pythonpython-sphinxdocstring

How to Correctly Display Pipe/Vertical Bar Character in Python Docstrings Using Sphinx


I'd like to put in a Python docstring something like:

output_dir
│   categories.yaml
│
└───images
│       filename1.png
│       filename1.png
│       ...
│
└───masks
        filename1.png
        filename2.png
        ...

But this is visualized by Sphinx as:

output_dir │ categories.yaml │ └───images │ filename1.png │ filename1.png │ ... │ └───masks filename1.png filename2.png ...

Any idea how to do it?

Thanks!


Solution

  • Put the content in a literal block in the docstring (note the indentation and the double colon):

    """
    Here is the directory structure::
    
       output_dir
       │   categories.yaml
       │
       └───images
       │       filename1.png
       │       filename1.png
       │       ...
       │
       └───masks
               filename1.png
               filename2.png
               ...
    
    """
    

    To do the same without showing the ":" is enough to have a whitespace before "::", like:

    """
    Here is the directory structure::
    
            output_dir
            │   categories.yaml
            │
            └───images
            │       filename1.png
            │       filename1.png
            │       ...
            │
            └───masks
                    filename1.png
                    filename2.png
                    ...
        
        or ::
        
            output_dir
            │   categories.yaml
            │
            └───test
            │   └───images
            │   │       filename1.png
            │   │       filename1.png
            │   │       ...
            │   │
            │   └───masks
            │   │       filename1.png
            │   │       filename2.png
            │   │       ...
            │
            └───train
            └───val
    """
    

    This will show ":" after "structure" but not after "or".