What's the best way to convert py to ipynb in code? or How can I programmatically convert py to ipynb

14 Replies, 1534 Views

Honestly, I’d recommend `nbformat` if you want full control over the conversion process.

You can write a small script to parse the .py file and create cells dynamically. Here’s a snippet to get you started:
```python
import nbformat as nbf
nb = nbf.v4.new_notebook()
nb.cells.append(nbf.v4.new_code_cell("your code here"))
nbf.write(nb, "output.ipynb")
```

Bit more work, but super flexible.

Messages In This Thread



Users browsing this thread: 1 Guest(s)