On Nov 29, 2016, at 2:04 PM, [email protected] wrote:
Bryan,
If I go down the subclass (2nd) route, I can't seem to add any new attributes to the class. This working example that builds upon what I showed above:
#!/usr/bin/env python
import bokeh.models.widgets
import bokeh.plotting
import bokeh.io
import bokeh.layouts
class SLIDER(bokeh.models.widgets.Slider):
__subtype__ = ''
__view_model__ = 'Slider'
def __init__(self, start, end):
super(bokeh.models.widgets.Slider, self).__init__(start=start, end=end)
self.x = 5 # Can't do this!
p = bokeh.plotting.figure()
p.circle(x=[0], y=[0])
s = SLIDER(1, 10)
bokeh.io.curdoc().add_root(bokeh.layouts.layout([[p, s]]))
Which results in the following error:
AttributeError: unexpected attribute 'x' to SLIDER, possible attributes are callback, callback_policy, callback_throttle, disabled, end, height, name, orientation, sizing_mode, start, step, tags, title, value or width
It's not entirely clear how to best get around this. Choosing this route keeps everything neat and tidy within the class while returning an instance of a slider (your final suggestion), while simple, gets messy if there is more attributes/methods need to be added. Any suggestions would be greatly appreciated!
Best,
Sean
On Monday, November 28, 2016 at 1:01:54 AM UTC-5, Bryan Van de ven wrote:
Sean doesn't want a different JS implementation though, AFAICT, he just wants to customize the configuration of the existing one on the python side. This actually uncovers a blind spot in our (or at least in my) assumptions about subclassing Bokeh models. Namely that subclassing is equivalent to wanting to extend BokehJS, which is evidently not always true.
For completeness, if you want to subclass a Bokeh model, but keep the identical BokehJS model, the options are:
* provide a minimal __implementation__ that extends the BokehJS parent class trivially, so the new sublcass exists on the JS side too
* use the __subtype__ attribute to map the new python subclass to an already existing BokehJS class name
However, in truth, I'd actually suggest a completely different approach. Bokeh models are declarative, and completely determined by the values of their attributes. They typically have no other behaviours, with a couple of exceptions for "mega" models like Plot. if you are subclassing to add behaviour, I think you are asking for confusion. And if you are subclassing just to change the default configuration of a model (as you seem to be), then you don't actually need to subclass at all. I think a function would actually be much better:
def SLIDER(start, end, **kw):
return Slider(start=start, end=end, **kw)
Thanks,
Bryan
> On Nov 27, 2016, at 11:08 PM, Sarah Bird <[email protected]> wrote:
>
> Hi Sean,
>
> The error is referring to a lack of JS implementation. You need a model of the same name JS side. You can provide that by specifying __implementation__ python side. See docs: http://bokeh.pydata.org/en/latest/docs/user_guide/extensions.html
>
> Sarah Bird
> sb...@continuum.io
>
> On Nov 27, 2016, at 7:17 PM, sean...@gmail.com wrote:
>
>> Bryan,
>>
>> Here is a simple example where I am inheriting from the Slider class because I want a slider that requires a start and end value in order to instantiate:
>>
>> #!/usr/bin/env python
>>
>> import bokeh.models.widgets
>> import bokeh.plotting
>> import bokeh.io
>> import bokeh.layouts
>>
>> class SLIDER(bokeh.models.widgets.Slider):
>>
>> #__subtype__ = ''
>> #__view_model__ = 'Slider'
>>
>> def __init__(self, start, end):
>> super(bokeh.models.widgets.Slider, self).__init__(start=start, end=end)
>>
>> if __name__ == '__main__':
>> p = bokeh.plotting.figure()
>> p.circle(x=[0], y=[0])
>> s = SLIDER(1, 10)
>> bokeh.io.show(bokeh.layouts.layout([[p, s]]))
>>
>> When the two lines are commented out, I get the following Bokeh error:
>>
>> Bokeh Error
>>
>> Module `SLIDER' does not exists. The problem may be two fold. Either a model was requested that's available in an extra bundle, e.g. a widget, or a custom model was requested, but it wasn't registered before first usage.
>>
>>
>>
>> On Sunday, November 27, 2016 at 3:38:22 PM UTC-5, Bryan Van de ven wrote:
>> Hi Sean,
>>
>> I would not expect either of these to be needed under any normal circumstances, and in fact none of the examples in the repo or the user's guide make use of them. In brief, __view_model__ provides the BokehJS class name. Normally this is automatically inferred from the python class name, and so __view_model__ is only ever useful if they need to differ for some reason. And __subtype__ is even more esoteric, it provides a way for more than one python class to map to the same BokehJS class, but also round trip correctly to python. Currently this is only used for Plot and Figure and Chart (which all resolve to just "Plot" on the BokehJS side). None of these are intended are user-facing features.
>>
>> As a concrete example, LatexLabel example inherits from Label, and does not need these:
>>
>> http://bokeh.pydata.org/en/latest/docs/user_guide/extensions_gallery/latex.html#userguide-extensions-examples-latex
>>
>> Can you provide code samples? Perhaps there is something you are trying that just needs to be expressed in a different way, or that we could document better. (It's also possible that I am wrong, and that there is some scenario where they are needed, that I just can't imagine offhand.)
>>
>> Thanks,
>>
>> Bryan
>>
>>
>> > On Nov 24, 2016, at 9:27 PM, sean...@gmail.com wrote:
>> >
>> > I noticed that when I try to inherit from an existing class that I need to add the following attributes:
>> >
>> > __view_model__
>> > __subtype__
>> >
>> > Is there documentation on what view_model and subtype are and what are reasonable values? In my case, I'm inheriting from the Slider class but I would appreciate any guidance for handling any inheritance situations that involve these two attributes. Is there a list of possible values?
>> >
>> > Best,
>> > Sean
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
>> > To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.
>> > To post to this group, send email to bo...@continuum.io.
>> > To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/b512d18f-1480-40e2-a0ae-8a5e5b6c7edc%40continuum.io\.
>> > For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.
>> To post to this group, send email to bo...@continuum.io.
>> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/95de04eb-4a53-436e-95be-90a8b5f0225a%40continuum.io\.
>> For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
>
> --
> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.
> To post to this group, send email to bo...@continuum.io.
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/AD2313F7-0FAA-4395-A3B1-088BA4D89825%40continuum.io\.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/32a2c452-d14f-4a93-a349-6c27bb333e2d%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.