Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Application Source Code Files in MkDocs Code Block

How can I display existing source code files within MkDocs? I want to include the files directly from another GitHub repository in code blocks without reformatting them so updated files will be shown in the MkDocs document.

sample_code.py

    def fn():
        pass
like image 590
flywire Avatar asked Oct 29 '25 10:10

flywire


1 Answers

Using MkDocs with Snippets extension. Snippets and/or off-line processing require files to be available locally which is explained in the Pro Git book Git Tools Submodules section.

  1. Include full file path even if the file in the same folder:

index.md

.
```python
--8<-- "docs/sample_code.py"
```
.
  1. Create the source code file:

sample_code.py

def fn():
    pass
  1. Add the extension to mkdocs configuration file:

mkdocs.yml

site_name: Demo

markdown_extensions:
    - pymdownx.snippets:

nav:
    - Demo: index.md

Output

.

def fn():
    pass

.

like image 53
flywire Avatar answered Oct 31 '25 11:10

flywire