mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-17 08:57:28 +08:00
Update built-in notebooks to use the setup cell (#1076)
* Teach about the setup cell in the elixir_and_livebook notebook * Update built-in notebooks to use the setup cell
This commit is contained in:
parent
43380e62e4
commit
241ee94cba
8 changed files with 74 additions and 84 deletions
|
@ -47,11 +47,12 @@ Sometimes you need a dependency or two and notebooks are no exception to this.
|
||||||
In Livebook, you can use [`Mix.install/2`](https://hexdocs.pm/mix/Mix.html#install/2)
|
In Livebook, you can use [`Mix.install/2`](https://hexdocs.pm/mix/Mix.html#install/2)
|
||||||
to bring dependencies into your notebook! This approach is especially useful when
|
to bring dependencies into your notebook! This approach is especially useful when
|
||||||
sharing notebooks because everyone will be able to get the same dependencies.
|
sharing notebooks because everyone will be able to get the same dependencies.
|
||||||
Let's try this out:
|
|
||||||
|
|
||||||
**Note:** compiling dependencies may use a reasonable amount of memory. If you are
|
Installing dependencies is a one-off setup operation and for those we use a special
|
||||||
hosting Livebook, make sure you have enough memory allocated to the Livebook
|
setup cell. Copy the code below, then scroll to the very top of the notebook,
|
||||||
instance, otherwise the command below will fail.
|
double-click on "Notebook dependencies and setup", add and run the code.
|
||||||
|
|
||||||
|
<!-- livebook:{"force_markdown":true} -->
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
Mix.install([
|
Mix.install([
|
||||||
|
@ -59,9 +60,17 @@ Mix.install([
|
||||||
])
|
])
|
||||||
```
|
```
|
||||||
|
|
||||||
[Kino](https://github.com/elixir-nx/kino) is a library that
|
**Note:** compiling dependencies may use a reasonable amount of memory. If you are
|
||||||
allows you to control parts of Livebook directly from the Elixir
|
hosting Livebook, make sure you have enough memory allocated to the Livebook
|
||||||
code. Let's use it to print a data table:
|
instance, otherwise the command below will fail.
|
||||||
|
|
||||||
|
<!-- livebook:{"break_markdown":true} -->
|
||||||
|
|
||||||
|
### Kino
|
||||||
|
|
||||||
|
The package we installed is [Kino](https://github.com/elixir-nx/kino) - a library
|
||||||
|
that allows you to control parts of Livebook directly from the Elixir code. Let's
|
||||||
|
use it to print a data table:
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
data = [
|
data = [
|
||||||
|
@ -82,11 +91,6 @@ them, consolidates protocols, and more. Check
|
||||||
[its documentation](https://hexdocs.pm/mix/Mix.html#install/2)
|
[its documentation](https://hexdocs.pm/mix/Mix.html#install/2)
|
||||||
to learn more.
|
to learn more.
|
||||||
|
|
||||||
Finally, keep in mind that `Mix.install/2` can be called only once
|
|
||||||
per runtime, so if you need to modify the dependencies, you should
|
|
||||||
go to the notebook runtime configuration and **reconnect** the current
|
|
||||||
runtime. Let's learn how to do that.
|
|
||||||
|
|
||||||
## Runtimes
|
## Runtimes
|
||||||
|
|
||||||
Livebook has a concept of **runtime**, which in practice is an Elixir node responsible
|
Livebook has a concept of **runtime**, which in practice is an Elixir node responsible
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
# Introduction to Nx
|
# Introduction to Nx
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
Mix.install([
|
||||||
|
{:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
## Numerical Elixir
|
## Numerical Elixir
|
||||||
|
|
||||||
Elixir's primary numerical datatypes and structures are not optimized
|
Elixir's primary numerical datatypes and structures are not optimized
|
||||||
|
@ -71,20 +77,8 @@ type. To interact with them, Nx relies on tensor-aware operators rather
|
||||||
than `Enum.map/2` and `Enum.reduce/3`.
|
than `Enum.map/2` and `Enum.reduce/3`.
|
||||||
|
|
||||||
In this section, we'll look at some of the various tools for
|
In this section, we'll look at some of the various tools for
|
||||||
creating and interacting with tensors.
|
creating and interacting with tensors. The IEx helpers will assist our
|
||||||
|
exploration of the core tensor concepts.
|
||||||
To get started, we'll first need to include the Nx dependency.
|
|
||||||
In Livebook, we can simply use Mix install. At the time of this writing,
|
|
||||||
there's no Hex package for Nx, so we'll use a git
|
|
||||||
dependency, like this:
|
|
||||||
|
|
||||||
```elixir
|
|
||||||
Mix.install([
|
|
||||||
{:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}
|
|
||||||
])
|
|
||||||
```
|
|
||||||
|
|
||||||
The IEx helpers will assist our exploration of the core tensor concepts.
|
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
import IEx.Helpers
|
import IEx.Helpers
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
# Plotting with VegaLite
|
# Plotting with VegaLite
|
||||||
|
|
||||||
## Setup
|
```elixir
|
||||||
|
Mix.install([
|
||||||
|
{:vega_lite, "~> 0.1.2"},
|
||||||
|
{:kino, "~> 0.5.0"}
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
We need two libraries for plotting in Livebook:
|
We need two libraries for plotting in Livebook:
|
||||||
|
|
||||||
|
@ -10,14 +17,7 @@ We need two libraries for plotting in Livebook:
|
||||||
* The [`kino`](https://github.com/elixir-nx/kino) package
|
* The [`kino`](https://github.com/elixir-nx/kino) package
|
||||||
renders our specifications
|
renders our specifications
|
||||||
|
|
||||||
Let's install them:
|
Let's install them by running the setup cell above.
|
||||||
|
|
||||||
```elixir
|
|
||||||
Mix.install([
|
|
||||||
{:vega_lite, "~> 0.1.2"},
|
|
||||||
{:kino, "~> 0.5.0"}
|
|
||||||
])
|
|
||||||
```
|
|
||||||
|
|
||||||
When building graphics we make extensive use of the functions from `VegaLite`,
|
When building graphics we make extensive use of the functions from `VegaLite`,
|
||||||
so it's useful to alias the module as something shorter.
|
so it's useful to alias the module as something shorter.
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
# Building a chat app with Kino.Control
|
# Building a chat app with Kino.Control
|
||||||
|
|
||||||
## Setup
|
|
||||||
|
|
||||||
In this notebook, we will build a chat application using
|
|
||||||
[`kino`](https://github.com/livebook-dev/kino). Let's
|
|
||||||
install it and get started:
|
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
Mix.install([
|
Mix.install([
|
||||||
{:kino, "~> 0.5.0"}
|
{:kino, "~> 0.5.0"}
|
||||||
])
|
])
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
In this notebook, we will build a chat application using
|
||||||
|
[`kino`](https://github.com/livebook-dev/kino). First, let's
|
||||||
|
have a look at the building blocks that we will need!
|
||||||
|
|
||||||
## Kino.Control
|
## Kino.Control
|
||||||
|
|
||||||
In our [introduction to Kino](/explore/notebooks/intro-to-kino),
|
In our [introduction to Kino](/explore/notebooks/intro-to-kino),
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
# Custom Kinos with Elixir and JavaScript
|
# Custom Kinos with Elixir and JavaScript
|
||||||
|
|
||||||
## Setup
|
```elixir
|
||||||
|
Mix.install([
|
||||||
|
{:kino, "~> 0.5.0"}
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
Starting from version v0.5, Livebook allows developers to implement
|
Starting from version v0.5, Livebook allows developers to implement
|
||||||
their own kinos. This allows developers to bring their own ideas to
|
their own kinos. This allows developers to bring their own ideas to
|
||||||
|
@ -9,14 +15,6 @@ life and extend Livebook in unexpected ways.
|
||||||
There are two types of custom kinos: static, via `Kino.JS`, and dynamic,
|
There are two types of custom kinos: static, via `Kino.JS`, and dynamic,
|
||||||
via `Kino.JS.Live`. We will learn to implement both in this notebook.
|
via `Kino.JS.Live`. We will learn to implement both in this notebook.
|
||||||
|
|
||||||
First, let's install `kino`:
|
|
||||||
|
|
||||||
```elixir
|
|
||||||
Mix.install([
|
|
||||||
{:kino, "~> 0.5.0"}
|
|
||||||
])
|
|
||||||
```
|
|
||||||
|
|
||||||
## HTML rendering with Kino.JS
|
## HTML rendering with Kino.JS
|
||||||
|
|
||||||
The "hello world" of custom kinos is one that embeds and
|
The "hello world" of custom kinos is one that embeds and
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
# Introduction to Kino
|
# Introduction to Kino
|
||||||
|
|
||||||
## Setup
|
|
||||||
|
|
||||||
In this notebook we will explore the possibilities that
|
|
||||||
[`kino`](https://github.com/elixir-nx/kino) brings
|
|
||||||
into your notebooks. Kino can be thought of as Livebook's
|
|
||||||
friend that instructs it how to render certain widgets
|
|
||||||
and interact with them. Let's install it:
|
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
Mix.install([
|
Mix.install([
|
||||||
{:kino, "~> 0.5.0"}
|
{:kino, "~> 0.5.0"}
|
||||||
])
|
])
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
In this notebook we will explore the possibilities that
|
||||||
|
[`kino`](https://github.com/elixir-nx/kino) brings
|
||||||
|
into your notebooks. Kino can be thought of as Livebook's
|
||||||
|
friend that instructs it how to render certain widgets
|
||||||
|
and interact with them. You can see `kino` listed as a
|
||||||
|
dependency above, let's run the setup cell and get started!
|
||||||
|
|
||||||
<!-- livebook:{"branch_parent_index":0} -->
|
<!-- livebook:{"branch_parent_index":0} -->
|
||||||
|
|
||||||
## Kino.Input
|
## Kino.Input
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
# Multiplayer pong game from scratch
|
# Multiplayer pong game from scratch
|
||||||
|
|
||||||
## Setup
|
```elixir
|
||||||
|
Mix.install([
|
||||||
|
{:kino, "~> 0.5.0"}
|
||||||
|
])
|
||||||
|
```
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
In this notebook, we are going to learn more about
|
In this notebook, we are going to learn more about
|
||||||
[`Kino.Control`](https://hexdocs.pm/kino/Kino.Control.html)
|
[`Kino.Control`](https://hexdocs.pm/kino/Kino.Control.html)
|
||||||
|
@ -9,14 +15,6 @@ Specifically, we will be building the the [Pong](https://en.wikipedia.org/wiki/P
|
||||||
game directly in Livebook. Not only that, we will actually
|
game directly in Livebook. Not only that, we will actually
|
||||||
make it multiplayer!
|
make it multiplayer!
|
||||||
|
|
||||||
Let's get started and install `kino`:
|
|
||||||
|
|
||||||
```elixir
|
|
||||||
Mix.install([
|
|
||||||
{:kino, "~> 0.5.0"}
|
|
||||||
])
|
|
||||||
```
|
|
||||||
|
|
||||||
## Painting the scene
|
## Painting the scene
|
||||||
|
|
||||||
The first step in our game is to define our scene. The
|
The first step in our game is to define our scene. The
|
||||||
|
|
|
@ -1,5 +1,14 @@
|
||||||
# Runtime introspection with VegaLite
|
# Runtime introspection with VegaLite
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
Mix.install([
|
||||||
|
{:vega_lite, "~> 0.1.2"},
|
||||||
|
{:kino, "~> 0.5.0"}
|
||||||
|
])
|
||||||
|
|
||||||
|
alias VegaLite, as: Vl
|
||||||
|
```
|
||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
In this notebook, we will use `Kino` and `VegaLite`
|
In this notebook, we will use `Kino` and `VegaLite`
|
||||||
|
@ -7,23 +16,9 @@ to introspect and plot how our system behaves over
|
||||||
time. If you are not familiar with VegaLite, [read
|
time. If you are not familiar with VegaLite, [read
|
||||||
its introductory notebook](/explore/notebooks/intro-to-vega-lite).
|
its introductory notebook](/explore/notebooks/intro-to-vega-lite).
|
||||||
|
|
||||||
## Setup
|
You can see both dependencies listed in the setup cell above.
|
||||||
|
We also define a convenience shortcut for the `VegaLite` module,
|
||||||
Let's add `:vega_lite` and `:kino` as dependencies:
|
let's run the setup and we are ready to go!
|
||||||
|
|
||||||
```elixir
|
|
||||||
Mix.install([
|
|
||||||
{:vega_lite, "~> 0.1.2"},
|
|
||||||
{:kino, "~> 0.5.0"}
|
|
||||||
])
|
|
||||||
```
|
|
||||||
|
|
||||||
Let's also define a convenience shortcut for the
|
|
||||||
VegaLite module:
|
|
||||||
|
|
||||||
```elixir
|
|
||||||
alias VegaLite, as: Vl
|
|
||||||
```
|
|
||||||
|
|
||||||
## Connecting to a remote node
|
## Connecting to a remote node
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue