Unable to update datasource in Bokeh python









up vote
2
down vote

favorite












I am using Bokeh 1.0.1. I am unable to update the data source in the Update method i.e src.data.update(new_src.data) doesn't seem to work. Below is the full code.



def modify_doc(doc):

def create_dataset(df, resample='D'):
# Resample the data
src = df.resample(resample).mean()

# Reset index for hovering
src.reset_index(inplace=True)

return ColumnDataSource(src)

def create_plot(src):
# Blank plot with correct labels
p = figure(plot_width=700, plot_height=300, x_axis_type="datetime",
title = 'Variation of Pollution',
x_axis_label = 'Time', y_axis_label = 'Pollution (µg/m³)')

p.line(source=src, x='Date & Time', y='pm2.5', line_width=2,
color='firebrick', line_alpha=0.5, legend='Actual')

hover = HoverTool(tooltips=[('Pollution', '@pm2.5 µg/m³'),
('Air Temp', '@Air Temp. °C'),
('Temp', '(@Min. Temp.0.2f, @Max. Temp.0.2f) °C'),
('Dew Pt.', '@Dew Pt. °C'),
('Rainfall', '@Rainfall mm'),
('Wind Dir.', '@Wind Dir. °'),
('Wind Speed', '@Wind Speed km/hr'),
('Relative humidity', '(@Min. RH0.2f, @Max. RH0.2f) %')],
mode='vline')

p.add_tools(hover)
p.legend.click_policy = 'hide'

return p

# Update function takes three default parameters
def update(attr, old, new):
# Resampling list
re_list = ['D', 'W', 'M', 'A']

# Make a new dataset based on the selected carriers and the
# make_dataset function defined earlier
new_src = create_dataset(df,
resample = re_list[resample_button_group.active])


# Update the source used the quad glpyhs
src.data.update(new_src.data)



resample_button_group = RadioButtonGroup(labels=["Day", "Week", "Month", "Year"], active=1)
resample_button_group.on_change('active', update)

controls = WidgetBox(resample_button_group)

# Initial Plot
src = create_dataset(df)
p = create_plot(src.data)

layout = row(controls, p)
doc.add_root(layout)

# Set up an application
handler = FunctionHandler(modify_doc)
app = Application(handler)









share|improve this question



























    up vote
    2
    down vote

    favorite












    I am using Bokeh 1.0.1. I am unable to update the data source in the Update method i.e src.data.update(new_src.data) doesn't seem to work. Below is the full code.



    def modify_doc(doc):

    def create_dataset(df, resample='D'):
    # Resample the data
    src = df.resample(resample).mean()

    # Reset index for hovering
    src.reset_index(inplace=True)

    return ColumnDataSource(src)

    def create_plot(src):
    # Blank plot with correct labels
    p = figure(plot_width=700, plot_height=300, x_axis_type="datetime",
    title = 'Variation of Pollution',
    x_axis_label = 'Time', y_axis_label = 'Pollution (µg/m³)')

    p.line(source=src, x='Date & Time', y='pm2.5', line_width=2,
    color='firebrick', line_alpha=0.5, legend='Actual')

    hover = HoverTool(tooltips=[('Pollution', '@pm2.5 µg/m³'),
    ('Air Temp', '@Air Temp. °C'),
    ('Temp', '(@Min. Temp.0.2f, @Max. Temp.0.2f) °C'),
    ('Dew Pt.', '@Dew Pt. °C'),
    ('Rainfall', '@Rainfall mm'),
    ('Wind Dir.', '@Wind Dir. °'),
    ('Wind Speed', '@Wind Speed km/hr'),
    ('Relative humidity', '(@Min. RH0.2f, @Max. RH0.2f) %')],
    mode='vline')

    p.add_tools(hover)
    p.legend.click_policy = 'hide'

    return p

    # Update function takes three default parameters
    def update(attr, old, new):
    # Resampling list
    re_list = ['D', 'W', 'M', 'A']

    # Make a new dataset based on the selected carriers and the
    # make_dataset function defined earlier
    new_src = create_dataset(df,
    resample = re_list[resample_button_group.active])


    # Update the source used the quad glpyhs
    src.data.update(new_src.data)



    resample_button_group = RadioButtonGroup(labels=["Day", "Week", "Month", "Year"], active=1)
    resample_button_group.on_change('active', update)

    controls = WidgetBox(resample_button_group)

    # Initial Plot
    src = create_dataset(df)
    p = create_plot(src.data)

    layout = row(controls, p)
    doc.add_root(layout)

    # Set up an application
    handler = FunctionHandler(modify_doc)
    app = Application(handler)









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am using Bokeh 1.0.1. I am unable to update the data source in the Update method i.e src.data.update(new_src.data) doesn't seem to work. Below is the full code.



      def modify_doc(doc):

      def create_dataset(df, resample='D'):
      # Resample the data
      src = df.resample(resample).mean()

      # Reset index for hovering
      src.reset_index(inplace=True)

      return ColumnDataSource(src)

      def create_plot(src):
      # Blank plot with correct labels
      p = figure(plot_width=700, plot_height=300, x_axis_type="datetime",
      title = 'Variation of Pollution',
      x_axis_label = 'Time', y_axis_label = 'Pollution (µg/m³)')

      p.line(source=src, x='Date & Time', y='pm2.5', line_width=2,
      color='firebrick', line_alpha=0.5, legend='Actual')

      hover = HoverTool(tooltips=[('Pollution', '@pm2.5 µg/m³'),
      ('Air Temp', '@Air Temp. °C'),
      ('Temp', '(@Min. Temp.0.2f, @Max. Temp.0.2f) °C'),
      ('Dew Pt.', '@Dew Pt. °C'),
      ('Rainfall', '@Rainfall mm'),
      ('Wind Dir.', '@Wind Dir. °'),
      ('Wind Speed', '@Wind Speed km/hr'),
      ('Relative humidity', '(@Min. RH0.2f, @Max. RH0.2f) %')],
      mode='vline')

      p.add_tools(hover)
      p.legend.click_policy = 'hide'

      return p

      # Update function takes three default parameters
      def update(attr, old, new):
      # Resampling list
      re_list = ['D', 'W', 'M', 'A']

      # Make a new dataset based on the selected carriers and the
      # make_dataset function defined earlier
      new_src = create_dataset(df,
      resample = re_list[resample_button_group.active])


      # Update the source used the quad glpyhs
      src.data.update(new_src.data)



      resample_button_group = RadioButtonGroup(labels=["Day", "Week", "Month", "Year"], active=1)
      resample_button_group.on_change('active', update)

      controls = WidgetBox(resample_button_group)

      # Initial Plot
      src = create_dataset(df)
      p = create_plot(src.data)

      layout = row(controls, p)
      doc.add_root(layout)

      # Set up an application
      handler = FunctionHandler(modify_doc)
      app = Application(handler)









      share|improve this question















      I am using Bokeh 1.0.1. I am unable to update the data source in the Update method i.e src.data.update(new_src.data) doesn't seem to work. Below is the full code.



      def modify_doc(doc):

      def create_dataset(df, resample='D'):
      # Resample the data
      src = df.resample(resample).mean()

      # Reset index for hovering
      src.reset_index(inplace=True)

      return ColumnDataSource(src)

      def create_plot(src):
      # Blank plot with correct labels
      p = figure(plot_width=700, plot_height=300, x_axis_type="datetime",
      title = 'Variation of Pollution',
      x_axis_label = 'Time', y_axis_label = 'Pollution (µg/m³)')

      p.line(source=src, x='Date & Time', y='pm2.5', line_width=2,
      color='firebrick', line_alpha=0.5, legend='Actual')

      hover = HoverTool(tooltips=[('Pollution', '@pm2.5 µg/m³'),
      ('Air Temp', '@Air Temp. °C'),
      ('Temp', '(@Min. Temp.0.2f, @Max. Temp.0.2f) °C'),
      ('Dew Pt.', '@Dew Pt. °C'),
      ('Rainfall', '@Rainfall mm'),
      ('Wind Dir.', '@Wind Dir. °'),
      ('Wind Speed', '@Wind Speed km/hr'),
      ('Relative humidity', '(@Min. RH0.2f, @Max. RH0.2f) %')],
      mode='vline')

      p.add_tools(hover)
      p.legend.click_policy = 'hide'

      return p

      # Update function takes three default parameters
      def update(attr, old, new):
      # Resampling list
      re_list = ['D', 'W', 'M', 'A']

      # Make a new dataset based on the selected carriers and the
      # make_dataset function defined earlier
      new_src = create_dataset(df,
      resample = re_list[resample_button_group.active])


      # Update the source used the quad glpyhs
      src.data.update(new_src.data)



      resample_button_group = RadioButtonGroup(labels=["Day", "Week", "Month", "Year"], active=1)
      resample_button_group.on_change('active', update)

      controls = WidgetBox(resample_button_group)

      # Initial Plot
      src = create_dataset(df)
      p = create_plot(src.data)

      layout = row(controls, p)
      doc.add_root(layout)

      # Set up an application
      handler = FunctionHandler(modify_doc)
      app = Application(handler)






      python pandas bokeh rbokeh bokehjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 10:21

























      asked Nov 11 at 9:47









      Po Xin

      133




      133






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You should be able to update the line glyph directly.



          First, modify your plotting code to assign a name to the line glyph:



          pm_line = p.line(
          source=src,
          x='Date & Time',
          y='pm2.5',
          line_width=2,
          color='firebrick',
          line_alpha=0.5,
          legend='Actual',
          name='pm_line' # Add this!
          )


          Then in your update function, replace your existing update line with the following:



          pm_line = p.select_one('name':'pm_line')
          pm_line.data_source.data = new_src.data





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247505%2funable-to-update-datasource-in-bokeh-python%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You should be able to update the line glyph directly.



            First, modify your plotting code to assign a name to the line glyph:



            pm_line = p.line(
            source=src,
            x='Date & Time',
            y='pm2.5',
            line_width=2,
            color='firebrick',
            line_alpha=0.5,
            legend='Actual',
            name='pm_line' # Add this!
            )


            Then in your update function, replace your existing update line with the following:



            pm_line = p.select_one('name':'pm_line')
            pm_line.data_source.data = new_src.data





            share|improve this answer
























              up vote
              0
              down vote



              accepted










              You should be able to update the line glyph directly.



              First, modify your plotting code to assign a name to the line glyph:



              pm_line = p.line(
              source=src,
              x='Date & Time',
              y='pm2.5',
              line_width=2,
              color='firebrick',
              line_alpha=0.5,
              legend='Actual',
              name='pm_line' # Add this!
              )


              Then in your update function, replace your existing update line with the following:



              pm_line = p.select_one('name':'pm_line')
              pm_line.data_source.data = new_src.data





              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                You should be able to update the line glyph directly.



                First, modify your plotting code to assign a name to the line glyph:



                pm_line = p.line(
                source=src,
                x='Date & Time',
                y='pm2.5',
                line_width=2,
                color='firebrick',
                line_alpha=0.5,
                legend='Actual',
                name='pm_line' # Add this!
                )


                Then in your update function, replace your existing update line with the following:



                pm_line = p.select_one('name':'pm_line')
                pm_line.data_source.data = new_src.data





                share|improve this answer












                You should be able to update the line glyph directly.



                First, modify your plotting code to assign a name to the line glyph:



                pm_line = p.line(
                source=src,
                x='Date & Time',
                y='pm2.5',
                line_width=2,
                color='firebrick',
                line_alpha=0.5,
                legend='Actual',
                name='pm_line' # Add this!
                )


                Then in your update function, replace your existing update line with the following:



                pm_line = p.select_one('name':'pm_line')
                pm_line.data_source.data = new_src.data






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 19:55









                PJW

                616725




                616725



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247505%2funable-to-update-datasource-in-bokeh-python%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3