To be able to edit code and run cells, you need to run the notebook yourself. Where would you like to run the notebook?

This notebook takes about 4 minutes to run.

In the cloud (experimental)

Binder is a free, open source service that runs scientific notebooks in the cloud! It will take a while, usually 2-7 minutes to get a session.

On your computer

(Recommended if you want to store your changes.)

  1. Copy the notebook URL:
  2. Run Pluto

    (Also see: How to install Julia and Pluto)

  3. Paste URL in the Open box

Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1
8×6 Matrix{Float64}:
 0.152952  0.317444  0.499406  0.59357   0.544016  0.789891
 0.207553  0.254918  0.769188  0.330772  0.554502  0.23572
 0.446825  0.311704  0.224097  0.349835  0.716939  0.796401
 0.295613  0.679434  0.581999  0.583991  0.493083  0.445794
 0.612518  0.630537  0.965809  0.258765  0.16101   0.889553
 0.83228   0.263657  0.962005  0.550208  0.390799  0.930047
 0.522433  0.099023  0.214691  0.903252  0.670078  0.10847
 0.763665  0.969336  0.706865  0.448895  0.499512  0.879504
👀 Reading hidden code
A = rand(8,6)
12.8 μs

Example: matrix of scrubbers

Click and drag one of the numbers below to modify B.

👀 Reading hidden code
6.1 ms
0.20.30.50.60.50.80.20.30.80.30.60.20.40.30.20.30.70.80.30.70.60.60.50.40.60.61.00.30.20.90.80.31.00.60.40.90.50.10.20.90.70.10.81.00.70.40.50.9
👀 Reading hidden code
823 ms
missing
👀 Reading hidden code
8.5 μs

Example: select entries

Click and drag to select entries. Use Ctrl (Windows) or Cmd (Mac) to combine selections.

👀 Reading hidden code
277 μs
👀 Reading hidden code
114 ms
8×6 Matrix{Bool}:
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
which_entries1
👀 Reading hidden code
10.2 μs

Example: select entries with multiplier

Click and drag to select entries. Use Ctrl (Windows) or Cmd (Mac) to combine selections.

👀 Reading hidden code
237 μs
@bind which_entries2 SelectEntries(A)
👀 Reading hidden code
611 μs
1.0
👀 Reading hidden code
157 ms
8×6 Matrix{Float64}:
 0.152952  0.317444  0.499406  0.59357   0.544016  0.789891
 0.207553  0.254918  0.769188  0.330772  0.554502  0.23572
 0.446825  0.311704  0.224097  0.349835  0.716939  0.796401
 0.295613  0.679434  0.581999  0.583991  0.493083  0.445794
 0.612518  0.630537  0.965809  0.258765  0.16101   0.889553
 0.83228   0.263657  0.962005  0.550208  0.390799  0.930047
 0.522433  0.099023  0.214691  0.903252  0.670078  0.10847
 0.763665  0.969336  0.706865  0.448895  0.499512  0.879504
👀 Reading hidden code
40.0 ms

Example: show_matrix

👀 Reading hidden code
179 μs
show_matrix(A)
👀 Reading hidden code
15.8 μs

As a second argument, you can pass a Matrix{Bool} of elements that should be shown in red.

👀 Reading hidden code
210 μs
show_matrix(A, A .> .9)
👀 Reading hidden code
295 ms
show_matrix(A, (A .> .9) .| (A .< .1))
👀 Reading hidden code
208 ms
👀 Reading hidden code
108 μs

Appendix

To use this widgets in another notebook, copy all cells in this appendix to a new notebook.

👀 Reading hidden code
244 μs
using PlutoUI
👀 Reading hidden code
400 ms
import HypertextLiteral: @htl
👀 Reading hidden code
162 μs

Select entries

👀 Reading hidden code
4.9 ms
SelectEntries
👀 Reading hidden code
9.2 ms
unwrap (generic function with 1 method)
👀 Reading hidden code
554 μs
@bind dope SelectEntries(A99)
👀 Reading hidden code
618 μs
4×3 Matrix{Bool}:
 0  0  0
 0  0  0
 0  0  0
 0  0  0
dope
👀 Reading hidden code
10.5 μs
bounding_box (generic function with 1 method)
👀 Reading hidden code
1.0 ms
3×2 Matrix{Bool}:
 1  1
 1  1
 1  1
👀 Reading hidden code
37.8 μs
👀 Reading hidden code
9.3 μs
b88 = @bind omg SelectEntries(default=hello)
👀 Reading hidden code
17.6 ms
3×2 Matrix{Bool}:
 0  1
 1  1
 1  1
👀 Reading hidden code
32.3 ms
👀 Reading hidden code
254 ms
rows (generic function with 1 method)
👀 Reading hidden code
880 μs

scrub_matrix

👀 Reading hidden code
12.5 ms
scrub_matrix (generic function with 1 method)
function scrub_matrix(A::Matrix{<:Real}; kwargs...)
h = PlutoUI.combine() do Child
@htl("""
<div style=$(Dict(
"display" => "inline-grid",
"grid-template-columns" => "repeat($(size(A,2)), auto)",
))>$(
map(eachindex(A')) do I
init = A'[I]
Child(Scrubbable(default_range(init); default=init, kwargs...))
end
)</div>
""")
end

transformed_value(h) do input
unwrap(A, collect(input))
end
end
👀 Reading hidden code
434 ms
default_range (generic function with 1 method)
function default_range(x::Integer)
if x == 0
-10:10
elseif 0 < x <= 10
0:10
elseif -10 <= x < 0
-10:0
else
sort(round.([Int64], (0.0:0.1:2.0) .* x))
end
end
👀 Reading hidden code
878 μs
default_range (generic function with 2 methods)
function default_range(x::Any)
[-1,0,1]
end
👀 Reading hidden code
372 μs
default_range (generic function with 3 methods)
function default_range(x::Real) # not an integer
if x == 0
-1.0 : 0.1 : 1.0
elseif 0 < x < 1
between_zero_and_x = LinRange(0.0, x, 10)
between_x_and_one = LinRange(x, 1.0, 10)
[between_zero_and_x..., between_x_and_one[2:end]...]
else
sort(x .* up_or_down_one_order_of_magnitude)
end
end
👀 Reading hidden code
932 μs
👀 Reading hidden code
59.1 ms
4×3 Matrix{Float64}:
 -0.924915  -1.08274   0.0433853
 -0.961241  -1.58845  -0.45484
 -0.728023  -1.08203   0.453577
  1.33635   -1.8721    1.77285
A99 = randn(4,3)
👀 Reading hidden code
16.4 μs
−0.9−1.10.0−1.0−1.6−0.5−0.7−1.10.51.3−1.91.8
b2 = @bind result scrub_matrix(A99)
👀 Reading hidden code
126 ms
−0.9−1.10.0−1.0−1.6−0.5−0.7−1.10.51.3−1.91.8
b2
👀 Reading hidden code
8.1 μs
missing
result
👀 Reading hidden code
8.4 μs

Transform superwidget

👀 Reading hidden code
188 μs
import AbstractPlutoDingetjes: AbstractPlutoDingetjes, Bonds
👀 Reading hidden code
179 μs
transformed_value
transformed_value(transform::Function, widget::Any; [initial_value::Function])

Create a new widget that wraps around an existing one, with a value transformation.

This function creates a so-called high-level widget: it returns your existing widget, but with additional functionality. You can use it in your package

Example

A simple example to get the point accross:

function RepeatedTextSlider(text::String)
	transform = input -> repeat(text, input)
	new_widget = transformed_value(transform, PlutoUI.Slider(1:10))
	return new_widget
end

@bind greeting RepeatedTextSlider("hello")

# moving the slider to the right...

greeting == "hellohellohello"

screenshot of the above code in action


This function is very useful in combination with PlutoUI.combine. Let's enhance our previous example by adding a text box where the repeated text can be entered. If you have not used PlutoUI.combine yet, you should read about that first.

function RepeatedTextSlider()
	# Note that the input to `transform` is now a Tuple!
	# (This is the output of `PlutoUI.combine`)
	transform = input -> repeat(input[1], input[2])

	old_widget = PlutoUI.combine() do Child
		md""" $(Child(PlutoUI.TextField())) $(Child(PlutoUI.Slider(1:10)))"""
	end
	new_widget = transformed_value(transform, old_widget)
	return new_widget
end

screenshot of the above code in action

👀 Reading hidden code
1.8 ms
❌ You need to update Pluto to use this PlutoUI element.
👀 Reading hidden code
1.9 ms
TransformedWidget
👀 Reading hidden code
5.0 ms

show_matrix

👀 Reading hidden code
172 μs
using Images
👀 Reading hidden code
65.3 s
show_matrix (generic function with 2 methods)
function show_matrix(A::Matrix, red::Union{BitMatrix,Matrix{Bool}}=zeros(Bool, size(A)))
base = RGB.(Gray.(A))

base[red] .= RGB(1.0, 0.1, 0.1)

# some tricks to show the pixels more clearly:
s = max(size(A)...)
if s >= 20
min_size = 1200
factor = min(5,min_size ÷ s)
kron(base, ones(factor, factor))
else
base
end
end
👀 Reading hidden code
1.2 ms
👀 Reading hidden code
166 μs