Skip to content

Commit

Permalink
Merge pull request #3 from MichaelHatherly/mh/docfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pankgeorg authored Nov 26, 2021
2 parents a075180 + 86a8ebd commit 18058a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ An abstract package to be implemented by packages/people who create widgets (or
### `Bonds.initial_value`
The initial value of a bond. In a notebook containing `@bind x my_widget`, this will be used in two cases:
1. The value of `x` will be set to `x = PlutoAbstractDingetjes.initial_value(my_widget)` during the `@bind` call. This initial value will be used in cells that use `x`, until the widget is rendered in the browser and the first value is received.
1. The value of `x` will be set to `x = AbstractPlutoDingetjes.Bonds.initial_value(my_widget)` during the `@bind` call. This initial value will be used in cells that use `x`, until the widget is rendered in the browser and the first value is received.
2. When running a notebook file without Pluto, e.g. `shell> julia my_notebook.jl`, this value will be used for `x`.

When not overloaded for your widget, it defaults to returning `missing`.
Expand All @@ -23,15 +23,15 @@ end

Base.show(io::IO, m::MIME"text/html", s::MySlider) = show(io, m, HTML("<input type=range min=$(first(s.values)) step=$(step(s.values)) max=$(last(s.values))>"))

PlutoAbstractDingetjes.Bonds.initial_value(s::MySlider) = first(s.range)
AbstractPlutoDingetjes.Bonds.initial_value(s::MySlider) = first(s.range)

# Add the following for the same functionality on Pluto versions TODO and below. Will be ignored in newer Pluto versions. See the compat info below.
Base.get(s::MySlider) = first(s.range)

```

### `Bonds.transform_value`
Transform a value received from the browser before assigning it to the bound julia variable. In a notebook containing `@bind x my_widget`, Pluto will run `x = PlutoAbstractDingetjes.Bonds.transform_value(my_widget, \$value_from_javascript)`. Without this hook, widgets in JavaScript can only return simple types (numbers, dictionaries, vectors) into bound variables.
Transform a value received from the browser before assigning it to the bound julia variable. In a notebook containing `@bind x my_widget`, Pluto will run `x = AbstractPlutoDingetjes.Bonds.transform_value(my_widget, \$value_from_javascript)`. Without this hook, widgets in JavaScript can only return simple types (numbers, dictionaries, vectors) into bound variables.

When not overloaded for your widget, it defaults to returning the value unchanged, i.e. `x = \$value_from_javascript`.

Expand All @@ -43,7 +43,7 @@ end

Base.show(io::IO, m::MIME"text/html", s::MyVectorSlider) = show(io, m, HTML("<input type=range min=1 max=$(length(s.values))>"))

PlutoAbstractDingetjes.Bonds.transform_value(s::MySlider, value_from_javascript::Int) = s.values[value_from_javascript]
AbstractPlutoDingetjes.Bonds.transform_value(s::MySlider, value_from_javascript::Int) = s.values[value_from_javascript]
```

See https://github.com/JuliaPluto/PlutoUI.jl/issues/3#issuecomment-629724036
Expand Down
32 changes: 16 additions & 16 deletions src/AbstractPlutoDingetjes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
An abstract package to be implemented by packages/people who create widgets/*dingetjes* for Pluto. If you are just happy using Pluto to make cool stuff, you probably don't want to use this package directly.
Take a look at [`PlutoAbstractDingetjes.Bonds`](@ref).
Take a look at [`AbstractPlutoDingetjes.Bonds`](@ref).
"""
module AbstractPlutoDingetjes

Expand All @@ -29,7 +29,7 @@ end
is_supported_by_display(io::IO, f::Union{Function,Module})::Bool
```
Check whether the current runtime/display (Pluto) supports a given feature from `AbstractPlutoDingetjes` (i.e. is the Pluto version new enough). This function should be used inside a `Base.show` method. The first argument should be the `io` provided to the `Base.show` method, and the second argument is the feature to check.
Check whether the current runtime/display (Pluto) supports a given feature from `AbstractPlutoDingetjes` (i.e. is the Pluto version new enough). This function should be used inside a `Base.show` method. The first argument should be the `io` provided to the `Base.show` method, and the second argument is the feature to check.
You can use this information to:
- Error the show method of your widget if the runtime/display does not support the required features, or
Expand All @@ -47,7 +47,7 @@ function Base.show(io::IO, m::MIME"text/html", d::MyCoolDingetje)
if !(is_supported_by_display(io, Bonds.initial_value) && is_supported_by_display(io, Bonds.transform_value))
error("This widget does not work in the current version of Pluto.")
end
write(io, html"...")
end
Expand Down Expand Up @@ -99,7 +99,7 @@ export NotGiven, InfinitePossibilities

"""
The initial value of a bond. In a notebook containing `@bind x my_widget`, this will be used in two cases:
1. The value of `x` will be set to `x = PlutoAbstractDingetjes.initial_value(my_widget)` during the `@bind` call. This initial value will be used in cells that use `x`, until the widget is rendered in the browser and the first value is received.
1. The value of `x` will be set to `x = AbstractPlutoDingetjes.Bonds.initial_value(my_widget)` during the `@bind` call. This initial value will be used in cells that use `x`, until the widget is rendered in the browser and the first value is received.
2. When running a notebook file without Pluto, e.g. `shell> julia my_notebook.jl`, this value will be used for `x`.
When not overloaded for your widget, it defaults to returning `missing`.
Expand All @@ -112,7 +112,7 @@ end
Base.show(io::IO, m::MIME"text/html", s::MySlider) = show(io, m, HTML("<input type=range min=\$(first(s.values)) step=\$(step(s.values)) max=\$(last(s.values))>"))
PlutoAbstractDingetjes.Bonds.initial_value(s::MySlider) = first(s.range)
AbstractPlutoDingetjes.Bonds.initial_value(s::MySlider) = first(s.range)
# Add the following for the same functionality on Pluto versions TODO and below. Will be ignored in future Pluto versions. See the compat info below.
Base.get(s::MySlider) = first(s.range)
Expand All @@ -124,10 +124,10 @@ Base.get(s::MySlider) = first(s.range)
!!! compat "Pluto TODO"
This feature only works in Pluto version TODO: NOT RELEASED YET or above.
This feature only works in Pluto version TODO: NOT RELEASED YET or above.
Older versions of Pluto used a `Base.get` overload for this (to avoid the need for the `AbstractPlutoDingetjes` package, but we changed our minds 💕). To support all versions of Pluto, use both methods of declaring the initial value.
Use [`AbstractPlutoDingetjes.is_supported_by_display`](@ref) if you want to check support inside your widget.
"""
Expand All @@ -137,7 +137,7 @@ initial_value(bond::Any) = missing


"""
Transform a value received from the browser before assigning it to the bound julia variable. In a notebook containing `@bind x my_widget`, Pluto will run `x = PlutoAbstractDingetjes.Bonds.transform_value(my_widget, \$value_from_javascript)`. Without this hook, widgets in JavaScript can only return simple types (numbers, dictionaries, vectors) into bound variables.
Transform a value received from the browser before assigning it to the bound julia variable. In a notebook containing `@bind x my_widget`, Pluto will run `x = AbstractPlutoDingetjes.Bonds.transform_value(my_widget, \$value_from_javascript)`. Without this hook, widgets in JavaScript can only return simple types (numbers, dictionaries, vectors) into bound variables.
When not overloaded for your widget, it defaults to returning the value unchanged, i.e. `x = \$value_from_javascript`.
Expand All @@ -149,12 +149,12 @@ end
Base.show(io::IO, m::MIME"text/html", s::MyVectorSlider) = show(io, m, HTML("<input type=range min=1 max=\$(length(s.values))>"))
PlutoAbstractDingetjes.Bonds.transform_value(s::MySlider, value_from_javascript::Int) = s.values[value_from_javascript]
AbstractPlutoDingetjes.Bonds.transform_value(s::MySlider, value_from_javascript::Int) = s.values[value_from_javascript]
```
!!! compat "Pluto TODO"
This feature only works in Pluto version TODO: NOT RELEASED YET or above. Values are not transformed in older versions.
Use [`AbstractPlutoDingetjes.is_supported_by_display`](@ref) if you want to check support inside your widget.
"""
Expand Down Expand Up @@ -182,15 +182,15 @@ end
Base.show(io::IO, m::MIME"text/html", s::MySlider) = show(io, m, HTML("<input type=range min=\$(first(s.values)) step=\$(step(s.values)) max=\$(last(s.values))>"))
PlutoAbstractDingetjes.Bonds.possible_values(s::MySlider) = s.range
AbstractPlutoDingetjes.Bonds.possible_values(s::MySlider) = s.range
```
```julia
struct MyTextBox end
Base.show(io::IO, m::MIME"text/html", s::MyTextBox) = show(io, m, HTML("<input type=text>"))
PlutoAbstractDingetjes.Bonds.possible_values(s::MySlider) = PlutoAbstractDingetjes.Bonds.InfinitePossibilities()
AbstractPlutoDingetjes.Bonds.possible_values(s::MySlider) = AbstractPlutoDingetjes.Bonds.InfinitePossibilities()
```
!!! info "Note about `transform_value`"
Expand All @@ -204,7 +204,7 @@ possible_values(bond::Any) = NotGiven()


"""
Validate a value received from the browser before "doing the pluto thing". In a notebook containing `@bind x my_widget`, Pluto will run `PlutoAbstractDingetjes.Bonds.validate_value(my_widget, \$value_from_javascript)`. If the result is `false`, then the value from JavaScript is considered "invalid" or "insecure", and no further code will be executed.
Validate a value received from the browser before "doing the pluto thing". In a notebook containing `@bind x my_widget`, Pluto will run `AbstractPlutoDingetjes.Bonds.validate_value(my_widget, \$value_from_javascript)`. If the result is `false`, then the value from JavaScript is considered "invalid" or "insecure", and no further code will be executed.
This is a protection measure when using your widget on a public PlutoSliderServer, where people could write fake requests that set bonds to arbitrary values.
Expand All @@ -218,7 +218,7 @@ end
Base.show(io::IO, m::MIME"text/html", s::MySlider) = show(io, m, HTML("<input type=range min=\$(first(s.values)) step=\$(step(s.values)) max=\$(last(s.values))>"))
PlutoAbstractDingetjes.Bonds.validate_value(s::MySlider, x::Any) = x isa Real && first(s.range) <= x <= last(s.range)
AbstractPlutoDingetjes.Bonds.validate_value(s::MySlider, x::Any) = x isa Real && first(s.range) <= x <= last(s.range)
```
!!! info "Note about `transform_value`"
Expand All @@ -229,7 +229,7 @@ PlutoAbstractDingetjes.Bonds.validate_value(s::MySlider, x::Any) = x isa Real &&
!!! compat "Pluto TODO"
This feature only works in Pluto version TODO: NOT RELEASED YET or above.
"""
validate_value(bond::Any, input::Any) = true

Expand Down

0 comments on commit 18058a2

Please sign in to comment.