Use Nx operators in Livebook?

I’m relatively new to Elixir, but have been reading about translating to Nx from Numpy/PyTorch:
From Python to Elixir Machine Learning
Matrix multiplication from foundations

I was thinking that it would be helpful to be able to play around with Nx in Livebook. What I want is to be able to have the operators available, in the manner that they are available to defn functions, so that I can write in Livebook in a straightforward mathematical style (like Matlab). This is possible by having the module definition in a cell, with import Nx.Defn at the top:

But I am limited to working within this one cell. If I go outside of this cell, then I have to revert to using the pipeline style:

The way I can think to get what I want would be to wrap multiple cells in a module definition:

Then within this context, I’m working in “Matlab mode”. I’m wondering if this is currently possible by some means in Livebook? Or if not, maybe there’s some metaprogramming means of achieving this? I’m still reading up on Elixir basics, so it’s certainly possible I’m missing some fundamental understanding about how things work…

Thanks!

2 Likes

Hey @ds604, you can do use Nx.Defn.Kernel at the top of your notebook, this is going to replace default imports, such as +, as it would within defn. We don’t recommend this in the docs or elsewhere, but it can be neat for experimentation, like in your case : )

3 Likes

it works! thanks!

Is there a way to change Livebook CSS? (like an easy external way, like I have a CSS file sitting somewhere that overrides some of the default settings) Mainly I want the text and cells to be a bit denser, so I can fit more on the screen at once. But if there’s a way to go in and make other changes, I’d sort of like to modify a few things to match other environments I’m familiar with.

There is a view configuration in the bottom-right corner:

Code zen only shows dense code cells. You can also use “Custom” and show both Markdown and Code cells, it is going to hide the insert buttons, so things will be more dense. In those modes, you can still insert cells using the keyboard shortcuts : )

1 Like

Thanks! For more customized changes, it would take going into the application itself? I was trying to figure out what’s going on through the browser devtools, but it’s a little difficult to understand… I can see the different CSS files in the Assets folder of the application, and I just started learning about how Phoenix applications are set up, maybe there’s other stuff going on in the Components.

Another question I had: If I want to save a notebook out to some static format, for static HTML, I can copy the HTML and CSS to a file like this: Matlab mode - Livebook. But suppose I wanted to print this out: either from the original Livebook or from this static file, I only seem to get the first page. I tried printing selection, but that didn’t seem to work either.

I know there’s the export option for markdown, but is there an option for printing or exporting rendered output that I might be overlooking?

Are you referring to the various smart cells you can add to the livebook?

Those are kino components and because of the modular nature of the design they are each a self-contained library comparable to micro-frontends, so each manages its own style, e.g. kino_bumblebee css, kino_explorer css, kino_db css

Also, welcome to the community!

1 Like

For more customized changes, it would take going into the application itself?

@ds604 yeah, you would have to either change the source code, or use a browser extension to add custom CSS rules.

I know there’s the export option for markdown, but is there an option for printing or exporting rendered output that I might be overlooking?

That’s because the page height matches the screen vertically (the sidebar) and the notebook content is a separate scroll container. But there are more details to make the printing look well (and it gets tricky with iframes, which are used for charts, tables, etc). There is Explore print.css · Issue #605 · livebook-dev/livebook · GitHub Another issue in this space is Support for pandoc integration · Issue #1159 · livebook-dev/livebook · GitHub, but those are just ideas at the moment.

1 Like

The pandoc option sounds pretty interesting.

=================
I took some notes on this, maybe someone else comes across this trying to figure out how to make a printout just for now. For a quick-fix printing solution (that works well enough for a page where maybe you don’t need the iframes), in my static HTML file, if I:

  1. am in Chrome (doesn’t seem to work in Firefox)
  2. drag-select everything (cmd-A select didn’t work, but drag-select did)
  3. right-click Print (cmd-P seems to revert to single page)

I can get a full printout: https://ds604.neocities.org/elixir/Matlab%20mode_Livebook.pdf

Maybe the other option would have been to paste markdown with outputs to a markdown renderer, and print that. But the HTML rendered output includes the (b64) images. I guess you could pull them out and put them in the markdown…

The only thing that didn’t get included is the plot, which I can recover from the markdown and maybe put Vegalite in the HTML file to get it to render.