Neovim for ML
I started my professional career with a role somewhat similar to DS, but as time went on, I gravitated more towards a backend role. Partly because I enjoyed it more and partly because it was an area I hadn’t explored as much.
After several years of using PyCharm/VSCode, I switched to NeoVim. There was quite the adaptation period, but now I don’t regret making the change at all.
Notebooks are undoubtedly handy, though they can be chaotic and nonlinear. Despite their convenience, they present challenges such as difficulty in maintenance, production adaptation, and change tracking.
Neovim Plugins
Python dependencies
Neovim Config
Here’s my current Neovim configuration for running Python code in chunks, a bit like a notebook, but in my opinion, it is more versatile.
Note: I built my configuration from scratch (there could be errors I might have missed). I use LazyVim as a plugin manager.
|
|
Workflow
I open neovim, where I usually split the window. I place my Python script in
one side, and on the other side, I open the terminal with IPython
.
I use slime to send commands from the script window to IPython
. One of the
biggest setbacks comes when viewing graphics. Matplotlib is pretty good, but
managing the qt or gke windows can be tiresome. For this reason, I’ve switched
to plotly, as it opens your graphics in the browser. This way, you have all
your graphs in one spot, and they are interactive too - it’s a win-win.
On those rare occasions when I need to use someone else’s notebook, I use
jupytext
, which basically converts notebooks to other formats, including
Python scripts. When you open a notebook in neovim, this plugin will convert
the notebook into a Python script in the background, and when you save it, it
will convert it back into a notebook again - it works pretty well.
In the configuration above, I’ve included a set of settings in order to emulate
notebook cells using # %%
, which allows me to include this comment and, when
executing <leader>cc
, it will send all the content between these markers or
to the end of the file.
Honestly, I don’t need more than this.
In fact, I get many more advantages than using VSCode or Notebooks, as I have the full power of vim and my other plugins like ChatGPT, Copilot, CLIs, etc.
The good thing is that, in the end, you have a much easier to maintain Python script.
IPython
brings a wealth of useful features to the table. For example, it
enables the use of magic commands like %run script.py
. But that’s not all, it
also provides the capability to autoload scripts or dependencies,
making it a standout, dynamic tool. You can even use rich, if you
like.
I hope you find this helpful. This same solution could potentially be used, but instead of working on a Python script, you would do so on a markdown or quarto file. It’s all about trying out what works best for you.