Skip to content

Commit c7a63df

Browse files
committed
Initial commit
0 parents  commit c7a63df

File tree

13 files changed

+616
-0
lines changed

13 files changed

+616
-0
lines changed

.github/workflows/tests.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
strategy:
12+
matrix:
13+
os: [ubuntu-latest]
14+
python-version: ['3.10', '3.11', '3.12']
15+
runs-on: ${{ matrix.os }}
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install test dependencies
23+
run: |
24+
python -m pip install --upgrade pip wheel
25+
- name: Temporary installation
26+
run: python -m pip install -e .[dev,test]
27+
- name: Test with pytest
28+
run: |
29+
ipython -m pytest
30+
- name: Check types with mypy
31+
run: |
32+
mypy ipython_markdown_inspector
33+
- name: Build package
34+
run: |
35+
python -m build
36+
- name: Install package
37+
run: python -m pip install --find-links=dist --ignore-installed ipython_markdown_inspector
38+
- name: Pip check
39+
run: python -m pip check
40+
- name: Publish builds
41+
uses: actions/upload-artifact@v4
42+
with:
43+
name: ipython_markdown_inspector dist Python${{ matrix.python-version }} run ${{ github.run_number }}
44+
path: ./dist

.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.5.0
4+
hooks:
5+
- id: end-of-file-fixer
6+
- id: check-case-conflict
7+
- id: check-executables-have-shebangs
8+
- id: check-added-large-files
9+
- id: check-case-conflict
10+
- id: check-toml
11+
- id: check-yaml
12+
- id: debug-statements
13+
- id: check-builtin-literals
14+
- id: trailing-whitespace
15+
- id: check-merge-conflict
16+
- repo: https://github.com/astral-sh/ruff-pre-commit
17+
rev: v0.2.0
18+
hooks:
19+
- id: ruff
20+
args: ["--fix"]
21+
- id: ruff-format

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2024, Michał Krassowski
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# ipython-markdown-inspector
2+
3+
[![tests](https://github.com/krassowski/ipython-markdown-inspector/workflows/tests/badge.svg)](https://github.com/krassowski/ipython-markdown-inspector/actions?query=workflow%3A%22tests%22)
4+
![CodeQL](https://github.com/krassowski/ipython-markdown-inspector/workflows/CodeQL/badge.svg)
5+
[![pypi-version](https://img.shields.io/pypi/v/ipython-markdown-inspector.svg)](https://python.org/pypi/ipython-markdown-inspector)
6+
7+
IPython extension providing Inspection outputs as Markdown, enabling better integration with Jupyter Notebook and JupyterLab.
8+
Depends on [`docstring-to-markdown`](https://github.com/python-lsp/docstring-to-markdown).
9+
10+
![](https://raw.githubusercontent.com/krassowski/ipython-markdown-inspector/main/docs/demo.gif)
11+
12+
## Installation
13+
14+
Requires `IPython` 8.21 or newer (which requires Python 3.10 or newer).
15+
16+
```bash
17+
pip install ipython-markdown-inspector
18+
```
19+
20+
## Usage
21+
22+
To load an extension while IPython is running, use the `%load_ext` magic:
23+
24+
```ipython
25+
%load_ext ipython_markdown_inspector
26+
```
27+
28+
To load it each time IPython starts, list it in your [configuration file](https://ipython.readthedocs.io/en/stable/config/intro.html):
29+
30+
```python
31+
c.InteractiveShellApp.extensions = [
32+
'ipython_markdown_inspector'
33+
]
34+
```
35+
36+
After enabling the extension, both the contents of "Contextual Help" panel,
37+
and results of info requests such as `%run?` or `df?` will provide the output in Markdown format.

docs/demo.png

91.9 KB
Loading
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from functools import partial
2+
from typing import Any, List, Optional, Union
3+
4+
from IPython.core.interactiveshell import InteractiveShell
5+
from IPython.core.oinspect import OInfo
6+
7+
from ._hook_data import InspectorHookData
8+
from .formatter import as_markdown
9+
10+
11+
def hook(
12+
obj_or_data: Union[InspectorHookData, Any],
13+
info: Optional[OInfo] = None,
14+
*_,
15+
ipython: InteractiveShell,
16+
) -> str:
17+
if isinstance(obj_or_data, InspectorHookData):
18+
data = obj_or_data
19+
else:
20+
# fallback for IPython 8.21
21+
obj = obj_or_data
22+
detail_level = 0
23+
omit_sections: List[str] = []
24+
info_dict = ipython.inspector.info(
25+
obj, "", info=info, detail_level=detail_level
26+
)
27+
data = InspectorHookData(
28+
obj=obj,
29+
info=info,
30+
info_dict=info_dict,
31+
detail_level=detail_level,
32+
omit_sections=omit_sections,
33+
)
34+
return as_markdown(data)
35+
36+
37+
ORIGINAL_HOOK = None
38+
39+
40+
def load_ipython_extension(ipython: InteractiveShell):
41+
global ORIGINAL_HOOK
42+
ORIGINAL_HOOK = ipython.inspector.mime_hooks.get("text/markdown", None)
43+
ipython.inspector.mime_hooks["text/markdown"] = partial(hook, ipython=ipython)
44+
45+
46+
def unload_ipython_extension(ipython: InteractiveShell):
47+
if ORIGINAL_HOOK is None:
48+
del ipython.inspector.mime_hooks["text/markdown"]
49+
else:
50+
ipython.inspector.mime_hooks["text/markdown"] = ORIGINAL_HOOK
51+
52+
53+
__all__: List[str] = []
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
try:
2+
from IPython.core.oinspect import InspectorHookData # type: ignore
3+
except ImportError:
4+
# TODO: remove once we require a version which includes
5+
# https://github.com/ipython/ipython/pull/14342
6+
from ._ipython_patch import InspectorHookData
7+
8+
9+
__all__ = ["InspectorHookData"]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Any, TypedDict, Optional
2+
from dataclasses import dataclass
3+
4+
from IPython.core.oinspect import OInfo
5+
6+
7+
class InfoDict(TypedDict):
8+
type_name: Optional[str]
9+
base_class: Optional[str]
10+
string_form: Optional[str]
11+
namespace: Optional[str]
12+
length: Optional[str]
13+
file: Optional[str]
14+
definition: Optional[str]
15+
docstring: Optional[str]
16+
source: Optional[str]
17+
init_definition: Optional[str]
18+
class_docstring: Optional[str]
19+
init_docstring: Optional[str]
20+
call_def: Optional[str]
21+
call_docstring: Optional[str]
22+
subclasses: Optional[str]
23+
# These won't be printed but will be used to determine how to
24+
# format the object
25+
ismagic: bool
26+
isalias: bool
27+
isclass: bool
28+
found: bool
29+
name: str
30+
31+
32+
@dataclass
33+
class InspectorHookData:
34+
"""Data passed to the mime hook"""
35+
36+
obj: Any
37+
info: Optional[OInfo]
38+
info_dict: InfoDict
39+
detail_level: int
40+
omit_sections: list[str]

0 commit comments

Comments
 (0)