Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Livebook

curriculum-main/reading/livebook.livemd

Livebook

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:smart_animation, github: "brooklinjazz/smart_animation"}
])

Navigation

Home Report An Issue GitCode Editors

Review Questions

Upon completing this lesson, a student should be able to answer the following questions.

  • How do you evaluate LiveBook Elixir cells?
  • How do you format LiveBook Elixir cells?
  • How do you manipulate LiveBook Markdown cells?

Overview

LiveBook

Livebook is an interactive and collaborative code notebook for Elixir. It’s commonly used as a documentation tool, and this is the first course to use it so extensively for teaching Elixir.

Inside these interactive notebooks, we can write formatted content (like you’re reading right now) and execute Elixir code.

Markdown

Markdown is a lightweight markup language that allows you to write formatted content using only special symbols. It’s like writing a formatted google document with only text.


# Heading 1
## Heading 2
### Heading 3
#### Heading 4


**bold**
*italic*


1. First item
2. Second item
3. Third item

You can view the Markdown cheat-sheet for an overview of markdown syntax.

Markdown is everywhere! It’s even valid input for chat apps like Slack and Discord.

Cells

Livebooks are split into cells. There are markdown cells for formatted text, and elixir cells for Elixir code. There are also more advanced cells that are beyond the scope of this course.

Markdown Cells

As you hover and click your cursor on a markdown cell. You’ll notice each has a blue highlight on the left and some controls.

You can edit , move up, move down , and delete cells. You can also upload images .

While completing interactive reading material and exercises, you generally do not need to use these controls. However, it’s useful to be aware of them if you would like to use Livebook in your own time, or to resolve an issue if you accidentally manipulate a Livebook.

Double click on any markdown cell and you’ll see the markdown editor for that cell.

Elixir Cells

Elixir Cells are interactive code blocks that run Elixir code. These cells can be used to test and experiment with Elixir code, and the results of the code execution are displayed directly in the notebook. We’ll often use these cells to show examples, and have you complete exercises.

Here you can see an Elixir cell that adds two integers.

2 + 2

You can create an Elixir cell anywhere throughout this course if you want to experiment with some Elixir code. Just be careful not to break existing code examples or affect further examples be rebinding already bound variables.

To create an Elixir cell, hover your cursor between two cells and press the +Code button.

Livebook may change this UI, so if this is instruction is no longer accurate please speak to your instructor and/or Report An Issue

Your Turn

Create an Elixir code cell below. Run some Elixir code such as 1 + 2 in the cell.

Livebook Shortcuts

Click on the keyboard shortcuts icon on the sidebar to view available Livebook shortcuts depending on the current mode.

Navigation Mode Vs Insert Mode

Navigation mode is the default mode. If you are editing a Markdown cell or Elixir cell, then you are in insert mode.

For example, while in Navigation mode, you can evaluate every Elixir cell in a Livebook notebook by pressing e and then a.

LiveMarkdown

Livebook allows us to create .livemd files that power the interactive notebooks that you’ve read so far! .livemd stands for live markdown. Livebook persists the notebook information in the .livemd file.

You can see the changes made to these files in your git version control. This is often useful if you accidentally deleted part of a lesson or exercise and need to refer to the old version.

Edit A Markdown Cell

Hover and click on this markdown cell to see the controls. Click the edit button to see the Markdown content inside.

Click Me Then Press Edit

Evaluate An Elixir Cell

Elixir cells contain Elixir code. You can evaluate the cell to show the result of the code.

Click on an Elixir cell and press the Evaluate button to see the cell’s output.

Alternatively, you can press CTRL+Enter (Windows & Linux) or CMD+Enter (MacOS) while focused on the cell to re-evaluate it.

Evaluate the cell below. You should see 10 because 5 + 5 equals 10.

5 + 5

In Elixir, the last value will always be the return value

For example, this cell returns 10, not 5.

5
10

As you complete reading material, you’re expected to evaluate cells to see their output. Alternatively, you can press ea to evaluate every cell in a Livebook.

Evaluation Order

By default, Elixir cells evaluate in order when you run the ea command. Cells above automatically evaluate when you evaluate a cell below.

Here are two empty Elixir cells. By default, an empty cell returns nil.

Evaluate the first cell and notice that the cell below becomes Stale.

This means that if the second cell relies on any value in the first cell, it is now outdated.

If you change a cell, Evaluated changes to Evaluated* to indicate that the cell has changed since the last time it was evaluated.

Evaluate the Elixir cell below, then change 5 to 4 to see Evaluated change to Evaluated**. Re-evaluate the cell to see Evaluated* change to *Evaluated.

5

Sometimes order of evaluation can cause issues in Exercises by using a stale version of an Elixir cell. Make sure to evaluate a code cell whenever you make a change.

Formatting

You can format Elixir code using the CTRL+SHIFT+I (Windows & Linux) Keybinding or SHIFT+OPTION+F (MacOS).

Try uncommenting the following code (Remove the # character) then press CTRL+SHIFT+I (Windows & Linux) or SHIFT+OPTION+F (MacOS) to format the code.

Notice that the 1 + 1 code moves to the left.

# 1 + 1

If you have not evaluated any Elixir code cells, you may see a warning.

> You need to start a runtime (or evaluate a cell) to enable code formatting.

Ensure you evaluate any Elixir cell before attempting to use the code format command.

If you cannot format an Elixir cell, you can use this as a clue that you have a syntax error in your code. For example, the following code is invalid. Notice that you cannot use CTRL+SHIFT+I to format the code.

          1 +

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Livebook reading"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue GitCode Editors