# jupyterlab-python-bytecode

> JupyterLab extension to inspect Python Bytecode

Latest version **1.0.0** (published 2020-03-12) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install jupyterlab-python-bytecode
pnpm add jupyterlab-python-bytecode
yarn add jupyterlab-python-bytecode
bun add jupyterlab-python-bytecode
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-03-12 |
| First published | 2018-09-29 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 14 |
| Unpacked size | 29.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 62 |
| Author | Jeremy Tuloup |
| Maintainers | jtpio |
| Keywords | jupyter, jupyterlab, jupyterlab-extension |

## Links

- npm: https://www.npmjs.com/package/jupyterlab-python-bytecode
- Repository: https://github.com/jtpio/jupyterlab-python-bytecode
- Issues: https://github.com/jtpio/jupyterlab-python-bytecode/issues
- npm.io page: https://npm.io/package/jupyterlab-python-bytecode

## Dependencies (14)

- [react](https://npm.io/package/react.md) ~16.9.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [react-dom](https://npm.io/package/react-dom.md) ~16.9.0
- [@jupyterlab/apputils](https://npm.io/package/@jupyterlab/apputils.md) ^2.0.0
- [@jupyterlab/nbformat](https://npm.io/package/@jupyterlab/nbformat.md) ^2.0.0
- [@jupyterlab/services](https://npm.io/package/@jupyterlab/services.md) ^5.0.0
- [@jupyterlab/coreutils](https://npm.io/package/@jupyterlab/coreutils.md) ^4.0.0
- [@jupyterlab/docmanager](https://npm.io/package/@jupyterlab/docmanager.md) ^2.0.0
- [@jupyterlab/fileeditor](https://npm.io/package/@jupyterlab/fileeditor.md) ^2.0.0
- [@jupyterlab/application](https://npm.io/package/@jupyterlab/application.md) ^2.0.0
- [@jupyterlab/filebrowser](https://npm.io/package/@jupyterlab/filebrowser.md) ^2.0.0
- [react-syntax-highlighter](https://npm.io/package/react-syntax-highlighter.md) ^12.2.1
- [@jupyterlab/ui-components](https://npm.io/package/@jupyterlab/ui-components.md) ^2.0.0
- [@jupyterlab/settingregistry](https://npm.io/package/@jupyterlab/settingregistry.md) ^2.0.0

## Recent versions

- 1.0.0 (latest) — 2020-03-12
- 0.7.0 — 2019-07-06
- 0.6.0 — 2019-02-21
- 0.5.0 — 2019-02-19
- 0.4.0 — 2019-02-05
- 0.3.1 — 2018-11-11
- 0.3.0 — 2018-11-06
- 0.2.0 — 2018-11-01
- 0.1.3 — 2018-10-09
- 0.1.2 — 2018-10-03
- 0.1.1 — 2018-09-29
- 0.1.0 — 2018-09-29

## README

# jupyterlab-python-bytecode

![Github Actions Status](https://github.com/jtpio/jupyterlab-python-bytecode/workflows/CI/badge.svg)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/jtpio/jupyterlab-python-bytecode/stable?urlpath=lab/tree/doc/example.py)
[![npm](https://img.shields.io/npm/v/jupyterlab-python-bytecode.svg)](https://www.npmjs.com/package/jupyterlab-python-bytecode)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

JupyterLab extension to inspect Python Bytecode.

![screencast](./doc/live_update.gif)

## Try it online

Try the extension in your browser with Binder:

[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/jtpio/jupyterlab-python-bytecode/stable?urlpath=lab/tree/doc/example.py)

## Prerequisites

- JupyterLab 1.0+
- `ipykernel` or `xeus-python`

To install JupyterLab:

```bash
conda install -c conda-forge jupyterlab
```

## Installation

```bash
jupyter labextension install jupyterlab-python-bytecode
```

## Features

- Live Bytecode preview
- Choose the kernel for a file (if not already started). This allows comparing the bytecode output for different versions of Python.
- Check the `Avanced Settings Editor` to tweak some of the settings

## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md) to know how to contribute and setup a development environment.

## How it works

Disassembling the Python code is done by connecting to a kernel, and sending the following code for evaluation from the lab extension:

```python
import dis
dis.dis(code_to_evaluate)
```

[As mentioned in the documentation](https://docs.python.org/3/library/dis.html), there is not guarantee on the stability of the bytecode across Python versions:

> Bytecode is an implementation detail of the CPython interpreter. No guarantees are made that bytecode will not be added, removed, or changed between versions of Python. Use of this module should not be considered to work across Python VMs or Python releases.

## Example

For example, if the Python file contains the following lines:

```python
import math

print(math.pi)
```

The following code will be sent to the kernel for evaluation:

```python
import dis
dis.dis("""
import math

print(math.pi)
""")
```

Which will return (example for CPython 3.6.6):

```python
  1           0 LOAD_CONST               0 (0)
              2 LOAD_CONST               1 (None)
              4 IMPORT_NAME              0 (math)
              6 STORE_NAME               0 (math)

  3           8 LOAD_NAME                1 (print)
             10 LOAD_NAME                0 (math)
             12 LOAD_ATTR                2 (pi)
             14 CALL_FUNCTION            1
             16 POP_TOP
             18 LOAD_CONST               1 (None)
             20 RETURN_VALUE
```

### Comparing versions of CPython

If you have several versions of Python installed on your machine (let's say in different conda environments), you can use the extension to check how the bytecode might differ.

The following example illustrates the introduction of the new `CALL_METHOD` opcode introduced in CPython 3.7:

![python_comparison](./doc/py36_py37_comparison.gif)

### Comparing for and while loops

Original example from [Disassembling Python Bytecode, by Peter Goldsborough](http://www.goldsborough.me/python/low-level/2016/10/04/00-31-30-disassembling_python_bytecode/)

![for_while](./doc/for_while.gif)

---
_Source: https://npm.io/package/jupyterlab-python-bytecode · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
