janpfeifer / gonb

GoNB, a Go Notebook Kernel for Jupyter
https://github.com/janpfeifer/gonb
MIT License
643 stars 36 forks source link

Creating a package inside the notebook cells? #141

Open oderwat opened 6 days ago

oderwat commented 6 days ago

Hello @janpfeifer,

I just wondered if it is possible to create a package inside of the cells of a notebook. Would that be difficulty to implement?

How about something like %package something in the head of the cell to indicate that the code goes into a package. GONB then would create a directory named something and adds the code of all cells with that same package name into a file and prepends it with the package something header.

This package could then getting imported as all the others and help to write code that can easier be copied 1:1 into external projects.

janpfeifer commented 5 days ago

So, this is interesting and naturally would be cool to add.

But I would put this on the back of the list for now, because I believe it would be lots of work. Things it would require from the top of my head:

  1. Supporting multiple memories of the current Go definitions (one per defined package)
  2. Special handling for go get, go import and some hacks to make it work, that I'm not sure would work the same when building a package as opposed to main. Not sure.
  3. Extend the %ls, %rm and %reset special commands to handle packages.

(As always, if you want to take a stab at it be my guest).

As an alternative -- probably not what you want, but ... -- during the GoMLX project development, I worked the whole time with gonb opened, using go work use <local gomlx path>, so I could keep editing in GoLand and playing around in GoNB, without having to do anything. Even the auto-complete in GoNB would pick up changes I made in GoLand immediately.