pthom / imgui_bundle

Dear ImGui Bundle: an extensive set of Ready-to-use widgets and libraries, based on ImGui. Start your first app in 5 lines of code, or less. Whether you prefer Python or C++, this pack has your back!
https://pthom.github.io/imgui_bundle/
MIT License
667 stars 66 forks source link

Using pure python backend with test_engine #149

Open csuckow opened 10 months ago

csuckow commented 10 months ago

Hello! I am tinkering with the recent example for glfw and trying to integrate the test_engine by following the instructions here and here. I was trying to click the button from the example window via test engine, but ran into some difficulties. When calling imgui.test_engine.start, the application freezes. Is there something obvious that I'm missing?

(...)
def main():
    imgui.create_context()
    # initializing test_engine
    test_engine = imgui.test_engine.create_context()
    test_click_button = imgui.test_engine.register_test(test_engine, "Demo Tests", "click button")
    def test_open_popup_func(ctx: imgui.test_engine.TestContext) -> None:
        ctx.set_ref("Custom window")
        ctx.item_click("**/Hello")
    test_click_button.test_func = test_open_popup_func
    #imgui.test_engine.start(test_engine, imgui.get_current_context())   <---- the application freezes
    (...)
pthom commented 10 months ago

Hello,

Integrating ImGui TestEngine directly from python, and without using HelloImGui and ImmApp is very difficult.

ImGui Test Engine uses two different threads (one for the main gui, and one for the scenario runner). Your python code will be called from two separate threads, and this breaks the GIL!

HelloImGui and ImmApp handle this well by transferring the GIL between threads (from C++). Doing this from pure python seems difficult.

dcnieho commented 8 months ago

I ran into this too, #166. Two thoughts:

  1. Can you document this, perhaps in the docs that come up for imgui.test_engine.create_context() and imgui.test_engine.start()?
  2. If i understand correctly, runner_params.use_imgui_test_engine can only be set before the start of the app. Can you make it such that on each iteration your display functions check if its true, and if its true but there is not test engine context yet, make, register and start it?

Regarding the second: just an idea, no idea 1. how needed it is, 2. how useful it is.

pthom commented 8 months ago

Can you document this, perhaps in the docs that come up for imgui.test_engine.create_context() and imgui.test_engine.start()?

Yes, see https://github.com/pthom/imgui_bundle/commit/20eecc036b6e6e5d3affd2058feae46bd2ef0d63

If i understand correctly, runner_params.use_imgui_test_engine can only be set before the start of the app. Can you make it such that on each iteration your display functions check if its true, and if its true but there is not test engine context yet, make, register and start it?

I see no real use case for this.

However, I added the possibility to call register_test several times during the app lifetime.

https://github.com/pthom/imgui_bundle/commit/745ded62dc889a2f11a9c36f798fcea3643b8e88

dcnieho commented 8 months ago

Ok, thanks for those additions!

So the logic would be that, until the patch upstream is in, you'd have to decide whether the test engine should be started or not before run()ing the app. I have not seen any performance issue connected to this, so guess its ok.

pthom commented 8 months ago

The patch is already upstream

dcnieho commented 8 months ago

Ah super! Nice work!