Skip to content

Latest commit

 

History

History
302 lines (205 loc) · 8.93 KB

livebook.livemd

File metadata and controls

302 lines (205 loc) · 8.93 KB

Livebook

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:smart_animation, github: "brooklinjazz/smart_animation"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively, you can evaluate the Elixir cells as you read.

Livebook

This lesson is going to be very meta. We're talking about Livebook within a 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

Livebook allows us to create .livemd files that power the interactive notebooks that you've read so far!

.livemd stands for live 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 which allow for the creation of styled text using the formatting markers just described. 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.

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

Elixir cells contain Elixir code. Evaluate the cell to show the result.

You can click on an Elixir cell to see the Evaluate button. Alternatively you can evaluate an Elixir cell by clicking on it the cell and pressing CTRL+Enter (Windows & Linux) or CMD+Enter (MacOS).

Click on the Elixir cell below and then click Evaluate to see the result. You should see 10 because 5 + 5 equals 10.

5 + 5

The result of the cell is the last value in the cell. For example, this cell returns 10.

5
10

You can also evaluate every Elixir cell in a notebook by pressing e and then a. You need to be in Navigation mode. Navigation mode is the default mode. If you are editing a markdown cell or Elixir cell, then you are in Insert mode.

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

Evaluation

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

Formatting

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

Try uncommenting the following code (Remove the # character) then press ALT+SHIFT+F (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 ALT+SHIFT+F to format the code.

          1 +

Tests

Throughout this course, you will perform exercises with test feedback. Under the hood we use a project called ExUnit which you will use in the future to write tests.

Evaluate the Elixir cell below to see a failing test. Then change 4 to 5 to see a passing test.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "Exercise" do
    try do
      Process.flag(:trap_exit, true)
      five = 4
      assert five == 5
    catch
      error ->
        flunk("""
          Your solution threw the following error:

          #{inspect(error)}
        """)

      :exit, {error, {GenServer, message_type, [_pid, message, _timeout]}} ->
        flunk("""
            GenServer crashed with the following error:

            #{inspect(error)}

            When it received: #{inspect(message)} #{message_type}

            Likely you need to define the corresponding handler for #{inspect(message)}.

            Ensure you defined a handle_call/3, handle_info/2, or handle_cast/2 or appropriate handler function.

              def handle_call(:message, _from, state) do
                ...
              end

            Also ensure you call GenServer.call/2, GenServer.cast/2, or otherwise send the message correctly.

              GenServer.call(pid, :message)
        """)

      :exit, error ->
        flunk("""
          Unhandled exit with the following error:

          #{inspect(error)}
        """)
    after
      # all warnings and errors are printed to the previous Kino Frame
      # to avoid cluttering the test results display.
      Process.sleep(10)
      Kino.render(Kino.Markdown.new("### Test Results
<hr/>"))
    end
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
# Unfortunately, this results in duplication of warnings.
five = 4

Let's break down the test feedback.

1) test  (Assertion)
     reading/livebook.livemd#cell:5
     Assertion with == failed
     code:  assert five == 5
     left:  4
     right: 5
     stacktrace:
       reading/livebook.livemd#cell:7: (test)



Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 1 failure

Randomized with seed 613714

ExUnit tells you that the test failed.

Assertion with == failed

Then it may provide information about the test being run. You may not be familiar with this code, and that's perfectly OK.

For this example, the test asserted that the answer should have been 5.

code:  assert five == 5

Generally, left is the provided answer, and right is the expected answer.

left:  4
right: 5

Passing tests are more straightforward. Each . is a passing test.

.

Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 0 failures

Randomized with seed 736220

Up Next

Previous Next
Git Code Editors