innovationOUtside / nbev3devsim

Ev3DevSim ipywidget in Jupyter notebooks
Apache License 2.0
5 stars 0 forks source link

Alternative templates for the simulator configuration #3

Open psychemedia opened 4 years ago

psychemedia commented 4 years ago

It would be useful to support different simulation configuration views, eg views that do or don't include the chart view, or that allow the user to declare which sensors are displayed in the live chart view.

This could be done either by supporting different pre-defined templates specified via the %%sim_magic call, or by %%sim_magic parameters that toggle studio display elements on and off.

psychemedia commented 4 years ago

We can get a bit more flexibility in terms of passing arguments into the magic using a construction of the form:

from IPython.core.magic import (magics_class, line_cell_magic, Magics)

  @line_cell_magic
  @magic_arguments.magic_arguments()
  @magic_arguments.argument('--sim', '-s', default='roboSim',
     help='Simulator object.'
    )
  def sim_magic(self, line, cell):
    "Send code to simulator."
    args = magic_arguments.parse_argstring(self.sim_magic, line)
    try:
      self.shell.user_ns[args.sim].set_element("prog", cell)
    except:
      print(f'Is {args.sim} defined?')

This would then support invocations such as %%sim_magic, using the default roboSim, and %%sim_magic -s testSim.

I did think it would support something like the following decorator:

  @magic_arguments.argument('--coords', '-c',
      help='Comma separated coords and angle of the robot (no spaces).'
    )

and a call in the magic of the form:


   if args.coords is not None:
        coords = args.coords.split(',')
        display(coords)
        if len(coords) == 3:
          x = int(coords[0])
          y = int(coords[1])
          theta = int(coords[2])
          self.shell.user_ns[args.sim].js_init(f'setPos({x},{y},{theta})')

but setPos() appears not to be in scope and is unrecognised? sim also appears to be out of scope (eg called as sim.setRobotPos(parseFloat({}),parseFloat({}),parseFloat({}) / 180 * Math.PI);).

Note that jp_proxy_widget also supports a get_value_async(callback, js) which can run the js statement and then invoke a Python callback function on the response.