Nextjournal for Lectures

1.
Homework 4 Article

I imported the first IJulia notebook I found from your lecture:

In the arcticle, you can see, the following features:

  • Latex support, Plots and rich output
  • multiple languages in one notebook
  • possibility to exchange data between different languages
  • data upload
  • interaction

1.1.
Ship files directly in articles

First thing you can do is to upload the files directly to the notebook, and instead of paths directly reference the uploaded files in code + text:

1.2.
The same interaction as in IJulia

If you click on the article, you will see that it also uses Interact.jl for interactive plots! One needs to edit + run the article to actually use any sliders, though, since it needs a running Julia process (without it it will show "WebIO not detected").

There are a few options to have interactions even without an active runner, e.g. directly add a javascript cell. We can generate some data in Julia:

# generate some data in julia
N = 3142
x = Float64[]; y = Float64[]; z = Float64[]
for i in 0:(N-1)
     r = i * (N - i)
     push!(x, r * cos(i / 30))
     push!(y, r * sin(i / 30))
     push!(z, i)
end
(x, y, z) # output so that it's referencable

Then create a javascript cell and interact with linewidth (click & drag): 10 , or re-execute the julia cell with different code, and the plot will automatically update with the new data:

// Use Plotly directly in pure JavaScript, with the data from the Julia article
var data = nil
var pointCount = 3142, i, r, x = data[0], y = data[1], z = data[2]
Nextjournal.plot([{
  type: 'scatter3d', 
  mode: 'lines',
  x: x, y: y, z: z,
  opacity: 0.7,
  line: { width: linewidth, color: z, colorscale: 'Viridis'}
}]);

2.
Makie & Interact on the GPU

Nextjournal supports GPU runners, and you can also install Makie on those instances. I made a Makie demo article. The interaction with Interact works exactly as with other plotting packages, but is rendered on the servers GPU and sents the plots to the output cell:

2.1.
Makie with Cairo Backend

The Makie Cairo backend works without a GPU runtime, but is restricted to 2D plots right now.

The below cell was created by taking the runtime from the makie demo article. This avoids any complicated setup:


221.5s
Language:Julia
using AbstractPlotting, CairoMakie, StatsMakie, MakieThemes, CSV
data_folder = joinpath(dirname(pathof(MakieThemes)), "..", "data")
for dataset  (:www, :drivers, :mtcars, :diamonds)
  data = CSV.read(
    joinpath(data_folder, string(dataset, ".tsv")), 
    delim = '\t', allowmissing = :none
  )
  @eval $(dataset) = $data
end

AbstractPlotting.set_theme!(ggthemr(:fresh))

p1 = scatterlines(
  Data(www), :Minute, :Users,
  Group(color = :Measure, marker = :Measure),
  markersize = 2
)

p2 = plot(
  density, Data(mtcars),
  :mpg, Group(color = :cyl)
)

p3 = plot(
  Position.stack, histogram, Data(diamonds),
  :price, Group(color = :cut)
)

p4 = boxplot(Data(drivers), :Year, :Deaths);

vbox(hbox(p1, p2), hbox(p3, p4))

For a more general overview of how to program in nextjournal, consider watching this introduction video:

© 2018 Nextjournal GmbH