Hacker News new | ask | show | jobs
by goodside 1879 days ago
Until recently, I also thought this was a major problem. My old solution was to always pair .ipynb files with proper .py modules of the same name, so the .ipynb always starts with `%run foo.py` and just calls functions.

However, I recently started using VSCode Insiders the preview release of VSCode, which has amazing support for Jupyter notebooks in the editor. You can use your normally configured linters, auto-formatters, vi-mode keyboard shortcuts (major selling point for me). You even get legible, cell-aware diffs when comparing in git. Now, the edit/git workflow for .ipynb files is so close to parity I've stopped caring whether code is in a proper Python module or not, and I almost never run Jupyter Notebook or Jupyter Lab in the browser.

In some ways this is a loss, because it's nice to have project-wide linting and other tooling that only work with .py files, but using `%run` was always an imperfect abstraction. By default, `%run` executes modules in a new module namespace which is then copied over, so it's not exactly "paste into Jupyter" unless you do `%run -i`. Even then, it's limited by running all at once. Every cell in a typical Notebook is effectively a button that runs an `exec()` statement, and you can't achieve those semantics by calling Python functions.

1 comments

I've been using the VSCode Insider's release as well and have been loving it and the new native notebook features for all the reasons you've listed already.

That's an interesting solution. I believe this is similar to what Joel Grus does [0], except %s/jupyter/ipython.

[0] https://www.youtube.com/watch?v=7jiPeIFXb6U