Macro input examples

This chapter consists of a series of examples demonstrating how to ask for user input inside macros.

A tutorial on macro input parameter can be found here. The API documentation: input()

  1from sardana.macroserver.macro import Type, imacro
  2
  3
  4@imacro()
  5def ask_number_of_points(self):
  6    """asks user for the number of points"""
  7
  8    nb_points = self.input("How many points?", data_type=Type.Integer)
  9    self.input("How many points?", data_type=Type.Integer)
 10    self.output("Points: %i", nb_points)
 11
 12
 13@imacro()
 14def ask_for_moveable(self):
 15    """asks user for a motor"""
 16
 17    moveable = self.input("Which moveable?", data_type=Type.Moveable)
 18    self.output("You selected %s which is at %f", moveable, moveable.getPosition())
 19
 20
 21@imacro()
 22def ask_for_car_brand(self):
 23    """asks user for a car brand"""
 24
 25    car_brands = "Mazda", "Citroen", "Renault"
 26    car_brand = self.input("Which car brand?", data_type=car_brands)
 27    self.output("You selected %s", car_brand)
 28
 29
 30@imacro()
 31def ask_for_multiple_car_brands(self):
 32    """asks user for several car brands"""
 33
 34    car_brands = "Mazda", "Citroen", "Renault", "Ferrari", "Porche", "Skoda"
 35    car_brands = self.input(
 36        "Which car brand(s)?",
 37        data_type=car_brands,
 38        allow_multiple=True,
 39        title="Favorites",
 40    )
 41    self.output("You selected %s", ", ".join(car_brands))
 42
 43
 44@imacro()
 45def ask_peak(self):
 46    """asks user for peak current of points with a custom title"""
 47
 48    peak = self.input(
 49        "What is the peak current?", data_type=Type.Float, title="Peak selection"
 50    )
 51    self.output("You selected a peak of %f A", peak)
 52
 53
 54@imacro()
 55def ask_peak_v2(self):
 56    """asks user for peak current of points with a custom title,
 57    default value, label and units"""
 58
 59    label, unit = "peak", "mA"
 60    peak = self.input(
 61        "What is the peak current?",
 62        data_type=Type.Float,
 63        title="Peak selection",
 64        key=label,
 65        unit=unit,
 66        default_value=123.4,
 67    )
 68    self.output("You selected a %s of %f %s", label, peak, unit)
 69
 70
 71@imacro()
 72def ask_peak_v3(self):
 73    """asks user for peak current of points with a custom title,
 74    default value, label, units and ranges"""
 75
 76    label, unit = "peak", "mA"
 77    peak = self.input(
 78        "What is the peak current?",
 79        data_type=Type.Float,
 80        title="Peak selection",
 81        key=label,
 82        unit=unit,
 83        default_value=123.4,
 84        minimum=0.0,
 85        maximum=200.0,
 86    )
 87    self.output("You selected a %s of %f %s", label, peak, unit)
 88
 89
 90@imacro()
 91def ask_peak_v4(self):
 92    """asks user for peak current of points with a custom title,
 93    default value, label, units, ranges and step size"""
 94
 95    label, unit = "peak", "mA"
 96    peak = self.input(
 97        "What is the peak current?",
 98        data_type=Type.Float,
 99        title="Peak selection",
100        key=label,
101        unit=unit,
102        default_value=123.4,
103        minimum=0.0,
104        maximum=200.0,
105        step=5,
106    )
107    self.output("You selected a %s of %f %s", label, peak, unit)
108
109
110@imacro()
111def ask_peak_v5(self):
112    """asks user for peak current of points with a custom title,
113    default value, label, units, ranges, step size and decimal places"""
114
115    label, unit = "peak", "mA"
116    peak = self.input(
117        "What is the peak current?",
118        data_type=Type.Float,
119        title="Peak selection",
120        key=label,
121        unit=unit,
122        default_value=123.4,
123        minimum=0.0,
124        maximum=200.0,
125        step=5,
126        decimals=2,
127    )
128    self.output("You selected a %s of %f %s", label, peak, unit)