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

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    해남서초등학교 학교 연혁 학교 동문 외부 링크 둘러보기 메뉴북위 34° 34′ 21″ 동경 126° 35′ 50″ / 북위 34.5726385° 동경 126.5972670°  / 34.5726385; 126.5972670해남서초등학교eh