|
| 1 | +# decorator to generate the input of a function from console input |
| 2 | + |
| 3 | +import functools |
| 4 | +import inspect |
| 5 | +import time |
| 6 | + |
| 7 | +import panel as pn |
| 8 | + |
| 9 | +from oold.model import LinkedBaseModel |
| 10 | +from oold.model.v1 import LinkedBaseModel as LinkedBaseModel_v1 |
| 11 | +from oold.ui.panel.anywidget_vite.jsoneditor import OswEditor |
| 12 | + |
| 13 | + |
| 14 | +class HitlApp(pn.viewable.Viewer): |
| 15 | + def __init__(self, **params): |
| 16 | + super().__init__(**params) |
| 17 | + |
| 18 | + self.message = pn.pane.Markdown( |
| 19 | + """This is a human-in-the-loop application. |
| 20 | + Please fill in the required fields and click 'Save' to proceed.""" # noqa |
| 21 | + ) |
| 22 | + self.jsoneditor = OswEditor(max_height=500, max_width=800) |
| 23 | + |
| 24 | + self.save_btn_clicked = False |
| 25 | + self.save_btn = pn.widgets.Button( |
| 26 | + css_classes=["save_btn"], name="Save", button_type="primary" |
| 27 | + ) |
| 28 | + pn.bind(self.on_save, self.save_btn, watch=True) |
| 29 | + |
| 30 | + self._view = pn.Column( |
| 31 | + self.message, |
| 32 | + self.jsoneditor, |
| 33 | + # display jsoneditor value in a JSON pane for debugging |
| 34 | + # pn.pane.JSON(self.jsoneditor.param.value, theme="light"), |
| 35 | + self.save_btn, |
| 36 | + ) |
| 37 | + |
| 38 | + def on_save(self, event): |
| 39 | + # Handle the save event here |
| 40 | + self.save_btn_clicked = True |
| 41 | + |
| 42 | + def __panel__(self): |
| 43 | + return self._view |
| 44 | + |
| 45 | + |
| 46 | +global ui |
| 47 | +ui: HitlApp = None |
| 48 | + |
| 49 | + |
| 50 | +def entry_point(gui: bool = False, jupyter: bool = False): |
| 51 | + """ |
| 52 | + Decorator factory to initialize the OswEditor and |
| 53 | + serve it before entering a workflow entry point. |
| 54 | + Spins up a Panel server to display the UI and waits |
| 55 | + for it to be ready if option gui is true. |
| 56 | + """ |
| 57 | + |
| 58 | + def decorator(func): |
| 59 | + @functools.wraps(func) |
| 60 | + def wrapper(*args, **kwargs): |
| 61 | + global ui |
| 62 | + |
| 63 | + def cleanup(): |
| 64 | + """ |
| 65 | + Clean up the UI after the workflow is done. |
| 66 | + """ |
| 67 | + global ui |
| 68 | + ui.message.object = "Workflow completed. You can close the web ui now." |
| 69 | + ui.jsoneditor.visible = False |
| 70 | + ui.save_btn.visible = False |
| 71 | + if not jupyter: |
| 72 | + print("Stopping web ui...") |
| 73 | + |
| 74 | + time.sleep(1) |
| 75 | + server.stop() |
| 76 | + ui = None |
| 77 | + |
| 78 | + def run_threaded(): # func, *args, **kwargs): |
| 79 | + """ |
| 80 | + Run the function in a separate thread to avoid blocking the main thread. |
| 81 | + """ |
| 82 | + func(*args, **kwargs) |
| 83 | + cleanup() |
| 84 | + |
| 85 | + if gui and ui is None: |
| 86 | + # Initialize the OswEditor |
| 87 | + ui = HitlApp() |
| 88 | + |
| 89 | + if jupyter: |
| 90 | + # print("Running in Jupyter, using display() to show the UI.") |
| 91 | + import threading |
| 92 | + |
| 93 | + # thread = threading.Thread(target=func, args=args, kwargs=kwargs) |
| 94 | + thread = threading.Thread(target=run_threaded) |
| 95 | + |
| 96 | + # call ipython display function to show the UI |
| 97 | + display(ui.servable()) # noqa |
| 98 | + |
| 99 | + thread.start() |
| 100 | + # thread.join() |
| 101 | + else: |
| 102 | + print("Spinning up web ui...") |
| 103 | + server = pn.serve(ui, threaded=True) |
| 104 | + |
| 105 | + # wait for the UI to be ready |
| 106 | + while not ui.jsoneditor.ready: |
| 107 | + time.sleep(0.1) |
| 108 | + # print("Web ui is ready.") |
| 109 | + |
| 110 | + if jupyter: |
| 111 | + # run the function in a thread to avoid blocking the Jupyter notebook |
| 112 | + print( |
| 113 | + "Running in Jupyter, executing the function in a separate thread." |
| 114 | + ) |
| 115 | + result = None |
| 116 | + # result = func(*args, **kwargs) |
| 117 | + else: |
| 118 | + result = func(*args, **kwargs) |
| 119 | + |
| 120 | + # Clean up after the workflow is done |
| 121 | + if gui and not jupyter: |
| 122 | + cleanup() |
| 123 | + |
| 124 | + return result |
| 125 | + |
| 126 | + return wrapper |
| 127 | + |
| 128 | + return decorator |
| 129 | + |
| 130 | + |
| 131 | +def hitl(func): |
| 132 | + """ |
| 133 | + Decorator to generate the input of a function from console input. |
| 134 | + """ |
| 135 | + |
| 136 | + @functools.wraps(func) |
| 137 | + def wrapper(*args, **kwargs): |
| 138 | + # Get the function's signature |
| 139 | + signature = inspect.signature(func) |
| 140 | + |
| 141 | + # Prepare a dictionary to hold the inputs |
| 142 | + inputs = {} |
| 143 | + global ui |
| 144 | + # Iterate over the parameters in the signature |
| 145 | + # ToDo: DataClass or Pydantic model support |
| 146 | + for param in signature.parameters.values(): |
| 147 | + # if parameter is a OOLD model run a jsoneditor |
| 148 | + if issubclass(param.annotation, LinkedBaseModel) or issubclass( |
| 149 | + param.annotation, LinkedBaseModel_v1 |
| 150 | + ): |
| 151 | + # If parameter is a model, use the OswEditor to get the value |
| 152 | + if ui is None: |
| 153 | + ui = HitlApp() |
| 154 | + pn.serve(ui, threaded=True) |
| 155 | + # wait for the UI to be ready |
| 156 | + while not ui.jsoneditor.ready: |
| 157 | + # print("Waiting for JSONEditor to be ready...") |
| 158 | + time.sleep(0.1) |
| 159 | + # print("Setting schema for parameter: ", param.name) |
| 160 | + ui.jsoneditor.set_schema(param.annotation.model_json_schema()) |
| 161 | + |
| 162 | + while not ui.save_btn_clicked: |
| 163 | + # print("Waiting for user input...") |
| 164 | + time.sleep(0.1) |
| 165 | + ui.save_btn_clicked = False # reset the button state |
| 166 | + inputs[param.name] = param.annotation(**ui.jsoneditor.get_value()) |
| 167 | + # continue |
| 168 | + elif param.default is param.empty: |
| 169 | + # If parameter has no default, prompt for input |
| 170 | + user_input = input( |
| 171 | + f"Enter value for {param.name} ({param.annotation}): " |
| 172 | + ) |
| 173 | + inputs[param.name] = user_input |
| 174 | + else: |
| 175 | + # If parameter has a default, use it |
| 176 | + inputs[param.name] = param.default |
| 177 | + |
| 178 | + # Call the original function with the collected inputs |
| 179 | + return func(*inputs.values()) |
| 180 | + |
| 181 | + return wrapper |
| 182 | + |
| 183 | + |
| 184 | +# Example usage |
| 185 | +@hitl |
| 186 | +def example_function(name: str, age: int = 30): |
| 187 | + print(f"Name: {name}, Age: {age}") |
0 commit comments