How to use a toggle button with a callback (in an class) in PyViz?How to merge two dictionaries in a single expression?How do I check if a list is empty?Are static class variables possible in Python?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?

Dobbs Murder Mystery : A Picture worth 1000 words?

Will this creature from Curse of Strahd reappear after being banished?

Translation of ει μη

Why were contact sensors put on three of the Lunar Module's four legs? Did they ever bend and stick out sideways?

What container to use to store developer concentrate?

Telling manager project isn't worth the effort?

Dual-national, returning to US the day the US Passport expires; can he check in with airline on Dutch passport but reenter with expiring US passport?

Why did some Apollo missions carry a grenade launcher?

Why didn’t Christianity spread southwards from Ethiopia in the Middle Ages?

ECDSA: Why is SigningKey shorter than VerifyingKey

Do 3/8 (37.5%) of Quadratics Have No x-Intercepts?

Japanese reading of an integer

Why is it "on the inside" and not "in the inside"?

How can Paypal know my card is being used in another account?

Why did Windows 95 crash the whole system but newer Windows only crashed programs?

Name These Animals

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

How could Nomadic scholars effectively memorize libraries worth of information

Sci-fi change: Too much or Not enough

Is there an antonym for "spicy" or "hot" regarding food?

What are the cons of stateless password generators?

Incrementing add under condition in pandas

2 weeks and a tight budget to prepare for Z-day. How long can I hunker down?

Are the named pipe created by `mknod` and the FIFO created by `mkfifo` equivalent?



How to use a toggle button with a callback (in an class) in PyViz?


How to merge two dictionaries in a single expression?How do I check if a list is empty?Are static class variables possible in Python?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am using PyViz / Panel in a notebook. Now I want to use a toggle button inside a class, and bind a callback to it.



This code - outside a class - is working:



import panel as pn
import panel.widgets as pnw
pn.extension()

toggle = pn.widgets.Toggle(name='Toggle')

def callback(*events):
if toggle.active is True: toggle.name = 'Active'
else: toggle.name = 'Toggle'

watcher = toggle.param.watch(callback, 'active')
toggle.param.set_param(active=False)
toggle.param.trigger('active')

pn.Row(toggle)


It produces a toggle button, and when clicked, the text changes.



No I tried to put everything inside a class definition:



class ToggleInClass():
def __init__(self):
self.toggle = pn.widgets.Toggle(name='Toggle')
self.watcher = self.toggle.param.watch(callback, 'active')
self.toggle.param.set_param(active=False)
self.toggle.param.trigger('active')

def callback(self, *events):
if toggle.active is True: toggle.name = 'Active'
else: toggle.name = 'Toggle'

toggle_in_class = ToggleInClass()
pn.Row(toggle_in_class.toggle)


Again a button is produced, but this time the callback does not seem to work: The text never changes.



The watcher seems to be ok:
toggle_in_class.watcher gives Watcher(inst=Toggle(), cls=<class 'panel.widgets.Toggle'>, fn=<function callback at 0x000001EC8419E510>, mode='args', onlychanged=True, parameter_names=('active',)).



The output of toggle_in_class.toggle.active alternates according the toggle state.



What is wrong with my callback / class definition?










share|improve this question






























    0















    I am using PyViz / Panel in a notebook. Now I want to use a toggle button inside a class, and bind a callback to it.



    This code - outside a class - is working:



    import panel as pn
    import panel.widgets as pnw
    pn.extension()

    toggle = pn.widgets.Toggle(name='Toggle')

    def callback(*events):
    if toggle.active is True: toggle.name = 'Active'
    else: toggle.name = 'Toggle'

    watcher = toggle.param.watch(callback, 'active')
    toggle.param.set_param(active=False)
    toggle.param.trigger('active')

    pn.Row(toggle)


    It produces a toggle button, and when clicked, the text changes.



    No I tried to put everything inside a class definition:



    class ToggleInClass():
    def __init__(self):
    self.toggle = pn.widgets.Toggle(name='Toggle')
    self.watcher = self.toggle.param.watch(callback, 'active')
    self.toggle.param.set_param(active=False)
    self.toggle.param.trigger('active')

    def callback(self, *events):
    if toggle.active is True: toggle.name = 'Active'
    else: toggle.name = 'Toggle'

    toggle_in_class = ToggleInClass()
    pn.Row(toggle_in_class.toggle)


    Again a button is produced, but this time the callback does not seem to work: The text never changes.



    The watcher seems to be ok:
    toggle_in_class.watcher gives Watcher(inst=Toggle(), cls=<class 'panel.widgets.Toggle'>, fn=<function callback at 0x000001EC8419E510>, mode='args', onlychanged=True, parameter_names=('active',)).



    The output of toggle_in_class.toggle.active alternates according the toggle state.



    What is wrong with my callback / class definition?










    share|improve this question


























      0












      0








      0


      1






      I am using PyViz / Panel in a notebook. Now I want to use a toggle button inside a class, and bind a callback to it.



      This code - outside a class - is working:



      import panel as pn
      import panel.widgets as pnw
      pn.extension()

      toggle = pn.widgets.Toggle(name='Toggle')

      def callback(*events):
      if toggle.active is True: toggle.name = 'Active'
      else: toggle.name = 'Toggle'

      watcher = toggle.param.watch(callback, 'active')
      toggle.param.set_param(active=False)
      toggle.param.trigger('active')

      pn.Row(toggle)


      It produces a toggle button, and when clicked, the text changes.



      No I tried to put everything inside a class definition:



      class ToggleInClass():
      def __init__(self):
      self.toggle = pn.widgets.Toggle(name='Toggle')
      self.watcher = self.toggle.param.watch(callback, 'active')
      self.toggle.param.set_param(active=False)
      self.toggle.param.trigger('active')

      def callback(self, *events):
      if toggle.active is True: toggle.name = 'Active'
      else: toggle.name = 'Toggle'

      toggle_in_class = ToggleInClass()
      pn.Row(toggle_in_class.toggle)


      Again a button is produced, but this time the callback does not seem to work: The text never changes.



      The watcher seems to be ok:
      toggle_in_class.watcher gives Watcher(inst=Toggle(), cls=<class 'panel.widgets.Toggle'>, fn=<function callback at 0x000001EC8419E510>, mode='args', onlychanged=True, parameter_names=('active',)).



      The output of toggle_in_class.toggle.active alternates according the toggle state.



      What is wrong with my callback / class definition?










      share|improve this question














      I am using PyViz / Panel in a notebook. Now I want to use a toggle button inside a class, and bind a callback to it.



      This code - outside a class - is working:



      import panel as pn
      import panel.widgets as pnw
      pn.extension()

      toggle = pn.widgets.Toggle(name='Toggle')

      def callback(*events):
      if toggle.active is True: toggle.name = 'Active'
      else: toggle.name = 'Toggle'

      watcher = toggle.param.watch(callback, 'active')
      toggle.param.set_param(active=False)
      toggle.param.trigger('active')

      pn.Row(toggle)


      It produces a toggle button, and when clicked, the text changes.



      No I tried to put everything inside a class definition:



      class ToggleInClass():
      def __init__(self):
      self.toggle = pn.widgets.Toggle(name='Toggle')
      self.watcher = self.toggle.param.watch(callback, 'active')
      self.toggle.param.set_param(active=False)
      self.toggle.param.trigger('active')

      def callback(self, *events):
      if toggle.active is True: toggle.name = 'Active'
      else: toggle.name = 'Toggle'

      toggle_in_class = ToggleInClass()
      pn.Row(toggle_in_class.toggle)


      Again a button is produced, but this time the callback does not seem to work: The text never changes.



      The watcher seems to be ok:
      toggle_in_class.watcher gives Watcher(inst=Toggle(), cls=<class 'panel.widgets.Toggle'>, fn=<function callback at 0x000001EC8419E510>, mode='args', onlychanged=True, parameter_names=('active',)).



      The output of toggle_in_class.toggle.active alternates according the toggle state.



      What is wrong with my callback / class definition?







      python widget pyviz






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 19:18









      mdkmdk

      1081 silver badge6 bronze badges




      1081 silver badge6 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0














          After correcting some stupid typos this code is working for me:



          class ToggleInClass():

          def __init__(self):
          self.toggle = pn.widgets.Toggle(name='Toggle')
          self.watcher = self.toggle.param.watch(self.callback, 'active')
          self.toggle.param.set_param(active=False)

          def callback(self, *events):
          if self.toggle.active is True: self.toggle.name = 'Active'
          else: self.toggle.name = 'Toggle'

          toggle_in_class = ToggleInClass()
          pn.Row(toggle_in_class.toggle)





          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',
            autoActivateHeartbeat: false,
            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%2f55364751%2fhow-to-use-a-toggle-button-with-a-callback-in-an-class-in-pyviz%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









            0














            After correcting some stupid typos this code is working for me:



            class ToggleInClass():

            def __init__(self):
            self.toggle = pn.widgets.Toggle(name='Toggle')
            self.watcher = self.toggle.param.watch(self.callback, 'active')
            self.toggle.param.set_param(active=False)

            def callback(self, *events):
            if self.toggle.active is True: self.toggle.name = 'Active'
            else: self.toggle.name = 'Toggle'

            toggle_in_class = ToggleInClass()
            pn.Row(toggle_in_class.toggle)





            share|improve this answer





























              0














              After correcting some stupid typos this code is working for me:



              class ToggleInClass():

              def __init__(self):
              self.toggle = pn.widgets.Toggle(name='Toggle')
              self.watcher = self.toggle.param.watch(self.callback, 'active')
              self.toggle.param.set_param(active=False)

              def callback(self, *events):
              if self.toggle.active is True: self.toggle.name = 'Active'
              else: self.toggle.name = 'Toggle'

              toggle_in_class = ToggleInClass()
              pn.Row(toggle_in_class.toggle)





              share|improve this answer



























                0












                0








                0







                After correcting some stupid typos this code is working for me:



                class ToggleInClass():

                def __init__(self):
                self.toggle = pn.widgets.Toggle(name='Toggle')
                self.watcher = self.toggle.param.watch(self.callback, 'active')
                self.toggle.param.set_param(active=False)

                def callback(self, *events):
                if self.toggle.active is True: self.toggle.name = 'Active'
                else: self.toggle.name = 'Toggle'

                toggle_in_class = ToggleInClass()
                pn.Row(toggle_in_class.toggle)





                share|improve this answer













                After correcting some stupid typos this code is working for me:



                class ToggleInClass():

                def __init__(self):
                self.toggle = pn.widgets.Toggle(name='Toggle')
                self.watcher = self.toggle.param.watch(self.callback, 'active')
                self.toggle.param.set_param(active=False)

                def callback(self, *events):
                if self.toggle.active is True: self.toggle.name = 'Active'
                else: self.toggle.name = 'Toggle'

                toggle_in_class = ToggleInClass()
                pn.Row(toggle_in_class.toggle)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 30 at 19:18









                mdkmdk

                1081 silver badge6 bronze badges




                1081 silver badge6 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364751%2fhow-to-use-a-toggle-button-with-a-callback-in-an-class-in-pyviz%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







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript