Utilities

class ntcore.util.ChooserControl(key: str, on_choices: Callable[[Sequence[str]], None] | None = None, on_selected: Callable[[str], None] | None = None, *, inst: NetworkTableInstance | None = None)[source]

Interacts with a wpilib.SendableChooser object over NetworkTables.

Parameters:
  • key – NetworkTables key

  • on_choices – A function that will be called when the choices change.

  • on_selection – A function that will be called when the selection changes.

  • inst – The NetworkTables instance to use.

close() None[source]

Stops listening for changes to the SendableChooser

getChoices() Sequence[str][source]

Returns the current choices. If the chooser doesn’t exist, this will return an empty tuple.

getSelected() str | None[source]

Returns the current selection or None

setSelected(selection: str) None[source]

Sets the active selection on the chooser

Parameters:

selection – Active selection name

ntcore.util.ntproperty(key: str, defaultValue, *, writeDefault: bool = True, doc: str = None, persistent: bool = False, type: NetworkTableType | None = None, inst: NetworkTableInstance | None = None) property[source]

A property that you can add to your classes to access NetworkTables variables like a normal variable.

Parameters:
  • key – A full NetworkTables key (eg /SmartDashboard/foo)

  • defaultValue (any) – Default value to use if not in the table

  • writeDefault – If True, put the default value to the table, overwriting existing values

  • doc – If given, will be the docstring of the property.

  • persistent – If True, persist set values across restarts. writeDefault is ignored if this is True.

  • type – Specify the type of this entry. If not specified, will autodetect the type from the default value

  • inst – The NetworkTables instance to use.

Example usage:

class Foo(object):

    something = ntproperty('/SmartDashboard/something', True)

    ...

    def do_thing(self):
        if self.something:    # reads from value
            ...

            self.something = False # writes value

Note

When using empty lists/tuples, you must explicitly specify the type.

Warning

This function assumes that the value’s type never changes. If it does, you’ll get really strange errors… so don’t do that.