My higher level goal was to use dash to take inputs and pass them to some robotics code. Along the way, I noticed some double processes going on, which probably aren’t worth getting into at the moment. For now, I’ll stick to this minimal example where I was able to reproduce:
#!/usr/bin/env python2 import dash import dash_core_components as dcc import dash_html_components as html class Foo: def __init__(self): print('someone created a foo!') class Gui: def __init__(self): self.app = dash.Dash() self.app.layout = html.Div(['test']) if __name__ == '__main__': print('in main') foo = Foo() gui = Gui() gui.app.run_server(debug=True)
I’m open to suggestions on a better way to do this, and fully admit to being fairly noobish to
python and definitely unfamiliar with classes. I wanted to create a class object with all of the
dash stuff, so that
__main__ could stay short. I’d create my
gui object, be able to start the server, and all of the layout code would be generated by some class methods. My
foo instance will store key variables for the robot and talk with the
dash object. In essence they’ll be updating each other, with the robot taking parameters from
dash plotting the data from the robot in real time. This was my stab at some way to accomplish this.
In the example above, putting dash in an object like this causes the main code to run twice, which surprised me. I get this in the terminal:
$ python class-test.py in main someone created a foo! * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit) * Restarting with stat in main someone created a foo! * Debugger is active! * Debugger PIN: 290-727-389
Why is the code evaluated twice?
Update in situ: as usual, as I’m writing what I think is a bug, I find out that it’s not at all a bug. So, how does one get
dash to play nicely when there’s other code and libraries going on? In my particular case, double running my
ROS code fails because it generates a node with a name, then tries to re-generate another node with the same name.
Perhaps to keep it simple, what if you had a class that did something with a file that shouldn’t exist, and you create it (or open it) upon initializing the object. Since pulling in
dash appears to run twice, this would bork that. I’m sure there’s some hacky ways to check that you’re really creating the object for the first time, but I’m guessing this behavior is known and there’s a better solution.