8

Livebook-driven development

 3 years ago
source link: https://goofansu.medium.com/livebook-driven-development-50f82e66fbff
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

What is Livebook?

Livebook is a notebook for writing Elixir and Markdown in an interactive way. We can evaluate Elixir code blocks and see the results immediately.

There are several methods to run Livebook, I choose the escript way.

> mix escript.install hex livebook
> livebook server
[Livebook] Application running at http://localhost:8080/?token=xxxxx

Livebook is running now! Just open it in a browser. Click the New Notebook button and start to write Elixir. With Elixir 1.12, you get the ability to run libraries after Mix.install/1 them.

How does Livebook benefit the development process?

Before Livebook, I write code in IEx, which is a REPL. It has some helpers to ease the way to explore code, but in my opinion, Livebook exceeds in two factors:

Code history

In fact, IEx can enable code history by setting export ERL_AFLAGS="-kernel shell_history enabled" in the shell profile file. You can also search the IEx code history with Ctrl-r and apply it. But as Livebook is essentially a notebook, you can see all texts and evaluation results without the need to set anything.

Visualization

Livebook has a clean UI. You can write documents in Markdown and evaluate Elixir code blocks. It is more continuous, you can review every step of your thought by scrolling the page.

How to develop in Livebook?

Use ogp as an example.

First, I explore the idea with code blocks.

  • Install library to parse HTML with Mix.install/1.
0*dMPqzh7bzd-P3TLM?q=20
livebook-driven-development-50f82e66fbff
  • Explore floki with Open Graph protocol.
0*gn3TRrHnLrLYNx7d?q=20
livebook-driven-development-50f82e66fbff
  • The variables in a code block can be referenced by blocks below it.
0*yhl9R_hD0Rme_o00?q=20
livebook-driven-development-50f82e66fbff
  • As going deeper, a simple parser comes out.
0*HRP6NYwsPTn1TggJ?q=20
livebook-driven-development-50f82e66fbff

Then I create an Elixir project and run the Livebook in the project.

  • Create an Elixir project with mix new ogp --module OpenGraph --sup.
  • Save the Livebook file in the project.
0*_suiGWeN2LRgqwhv?q=20
livebook-driven-development-50f82e66fbff
  • Move the parser into the project and run Livebook in Mix standalone mode.
0*CaeJIQz-TwdsNJ8u?q=20
livebook-driven-development-50f82e66fbff

Connection timed out occurs, to resolve it, run Livebook with a short name: livebook server --sname notebook. See https://github.com/elixir-nx/livebook/issues/275 for detail.

  • Set up Mix standalone mode again successfully, uses the module from the ogp project.
0*BEwovyULqoZr9F7_?q=20
livebook-driven-development-50f82e66fbff

Conclusion

Developing in Livebook is really delightful. It helps to explore the code and writing documents. Give it a try and you will love it!

PS: The Livebook file created in this story can be found here: https://gist.github.com/goofansu/650ef0e67277915ab36aa1acc0defe60


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK