Switching between data sources without modifying original

Below is an example where the intended behavior is a plot where the toggle button switches which CDS the plot uses for a data source. Neither CDS should be modified.
#! /usr/bin/env python
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Toggle
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import column
def switch_cds(active):
square = curdoc().select_one({‘name’:‘square’})
print “first cds contents before update:\n”, first.data
print “second cds contents before update:\n”, second.data
if active:
square.data_source.data = first.data
else:
square.data_source.data = second.data
print “first cds contents after update:\n”, first.data
print “second cds contents after update:\n”, second.data
p = figure()
xs = [0,1,2,3,4,5]
y1 = [0,1,2,3,4,5]
y2 = [0,-1,-2,-3,-4,-5]
first = ColumnDataSource(data=dict(
xs=xs,
ys=y1,
color=[‘blue’] * 6
))
second = ColumnDataSource(data=dict(
xs=xs,
ys=y2,
color=[‘blue’] * 6
))
p.square(‘xs’,‘ys’, color=‘color’, name=‘square’,source=first)
toggle = Toggle(active=True)
toggle.on_click(switch_cds)
curdoc().add_root(column(toggle,p))
However, after toggling the switch (i.e. running switch_cds once), the results show that despite not changing the first cds’ contents directly, the act of switching data source.data affects not only the plot, but the original cds. I suspect that data_source.data maintains a reference to the original CDS, and thus when I set data_source.data equal to a different CDS, it simply copies the contents, but this is as far as I can guess.
The intended behavior is to switch the two data sources without modifying the original. How can I accomplish this?

The data is a property of the data source, so your code is
modifying the data source.

To change the datasource it would be something like

if active:

        square.data_source = first_source

    else:

        square.data_source = second_source

However, I'm not sure that'll actually work.

Can you explain more about your requirement to not modify a data

source. I often swap data in and out, but always by modifying the
CDS.

Sincerely,

Sarah Bird
···

On 8/12/16 8:00 AM, Ari Krumbein wrote:

    Below is an example where the intended behavior is

a plot where the toggle button switches which CDS the plot uses
for a data source. Neither CDS should be modified.

      #! /usr/bin/env python

      from bokeh.models import ColumnDataSource
      from bokeh.models.widgets import Toggle
      from bokeh.plotting import figure
      from bokeh.io import curdoc
      from bokeh.layouts import column


      def switch_cds(active):
          square = curdoc().select_one({'name':'square'})

          print "first cds contents before update:\n", first.data
          print "second cds contents before update:\n", second.data

          if active:
              square.data_source.data = first.data
          else:
              square.data_source.data = second.data

          print "first cds contents after update:\n", first.data
          print "second cds contents after update:\n", second.data


      p = figure()
      xs = [0,1,2,3,4,5]
      y1 = [0,1,2,3,4,5]
      y2 = [0,-1,-2,-3,-4,-5]

      first = ColumnDataSource(data=dict(
          xs=xs,
          ys=y1,
          color=['blue'] * 6
          ))

      second = ColumnDataSource(data=dict(
          xs=xs,
          ys=y2,
          color=['blue'] * 6
          ))

      p.square('xs','ys', color='color', name='square',source=first)
      toggle = Toggle(active=True)
      toggle.on_click(switch_cds)
      curdoc().add_root(column(toggle,p))

    However, after toggling the switch (i.e. running switch_cds

once), the results show that despite not changing the first cds’
contents directly, the act of switching data source.data affects
not only the plot, but the original cds. I suspect that
data_source.data maintains a reference to the original CDS, and
thus when I set data_source.data
equal to a different CDS, it simply copies the contents,
but this is as far as I can guess.

    **          The intended behavior is to switch the two data sources

without modifying the original. How can I accomplish this?**

  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/6c28645a-1b82-461d-82a1-864e184b4bd2%40continuum.io](https://groups.google.com/a/continuum.io/d/msgid/bokeh/6c28645a-1b82-461d-82a1-864e184b4bd2%40continuum.io?utm_medium=email&utm_source=footer).

  For more options, visit [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout).


Sarah Bird
Developer, Bokeh

    [
      ![Continuum Analytics](http://docs.continuum.io/_static/img/ContinuumWordmark.png)
    ](http://continuum.io)