Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Widget.from_param classmethod #1344

Merged
merged 4 commits into from
May 18, 2020
Merged

Add Widget.from_param classmethod #1344

merged 4 commits into from
May 18, 2020

Conversation

philippjfr
Copy link
Member

@philippjfr philippjfr commented May 14, 2020

Simplifies the creation of a widget linked to and created from a particular parameter.

import param

class A(param.Parameterized):
    
    value = param.Number()
    
a = A()

pn.widgets.FloatSlider.from_param(a.param.value, start=0, end=3.14)

@maximlt @MarcSkovMadsen Any thoughts on this? It's a solution to something I think you both brought up before. If you like the idea maybe we should also consider a shorter method name than from_parameter, suggestions welcome.

@philippjfr philippjfr added the type: enhancement Minor feature or improvement to an existing feature label May 14, 2020
@codecov
Copy link

codecov bot commented May 14, 2020

Codecov Report

Merging #1344 into master will increase coverage by 0.19%.
The diff coverage is 33.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1344      +/-   ##
==========================================
+ Coverage   87.49%   87.69%   +0.19%     
==========================================
  Files         130      130              
  Lines       13758    13976     +218     
==========================================
+ Hits        12038    12256     +218     
  Misses       1720     1720              
Impacted Files Coverage Δ
panel/widgets/base.py 85.22% <33.33%> (-1.84%) ⬇️
panel/tests/test_param.py 99.88% <0.00%> (+0.02%) ⬆️
panel/param.py 92.77% <0.00%> (+1.32%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b73ec77...6f51b1d. Read the comment docs.

@jbednar
Copy link
Member

jbednar commented May 15, 2020

If you like the idea maybe we should also consider a shorter method name than from_parameter, suggestions welcome.

I got as far as the title on this page before my brain went (a) nice! and (b) can we have a shorter name? :-) How about just from, currently supporting Parameters but e.g. supporting a traitlet or some other source potentially? You could maybe also consider it another type of linking, and make .link() be either an instance or class method, where it does this when called on the class.

@maximlt
Copy link
Member

maximlt commented May 15, 2020

I don't remember if I ever brought this up but I'll still give you my 2 cents!

This

pn.widgets.FloatSlider.from_parameter(a.param.value, start=0, end=3.14)

is better than

pn.Param(a.param.value, widgets={"value": {"type": pn.widgets.FloatSlider, "start":0, "end":3.14}})

That's even better :-)

pn.widgets.FloatSlider.from(a.param.value, start=0, end=3.14)

As for the implementation, is it intended that it returns a Param pane and not a widget? I guess I'd expect a widget. It'd make it clearer that this method is intended to create a single widget linked to a parameter and that it would not overlap (too much) with what can already be achieved with the Param pane. It can indeed be finely customized if one sets the precedence of the parameters and/or uses the keyword args parameters and/or widgets. I have sometimes the feeling that there are too many ways to achieve the same thing in panel, just trying to avoid that.

@philippjfr
Copy link
Member Author

As for the implementation, is it intended that it returns a Param pane and not a widget?

No, I think it should return the widget as you point out.

@philippjfr
Copy link
Member Author

Oh and .from won't work since it's a reserved name in Python, any other suggestions?

@maximlt
Copy link
Member

maximlt commented May 18, 2020

Arf missed that. I like that the method starts with from, I'm sometimes confused with .link (that I don't use much TBH), by mistake I could write a.param.value.link(pn.widgets.FloatSlider, start=0, end=3.14)...
Explicit is better than implicit, from_param ? (+ from_trait later on?)

@philippjfr
Copy link
Member Author

Yeah, I'm also leaning towards .from_param, not a huge fan of overly generic methods.

@philippjfr philippjfr changed the title Add Widget.from_parameter classmethod Add Widget.from_param classmethod May 18, 2020
@philippjfr philippjfr merged commit b8b97ac into master May 18, 2020
@philippjfr philippjfr deleted the widget_from_parameter branch May 18, 2020 11:29
philippjfr added a commit that referenced this pull request May 24, 2020
* Add Widget.from_parameter classmethod

* Renamed to from_param

* Add docs

* Add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Minor feature or improvement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants