pthom / imgui_bundle

Your fast track to powerful GUIs: Dear ImGui Bundle, an extensive toolkit for Python and C++ with immediate mode efficiency.
https://pthom.github.io/imgui_bundle/
MIT License
703 stars 73 forks source link

imgui_bundle in python with asyncio #243

Closed BlindChickens closed 1 month ago

BlindChickens commented 3 months ago

As in the title, I want to make use of imgui_bundle in python with an asyncio app. There is one way to do it using two seperate threads, but I don't fully comprehend what the consequences of that will be going further with the app.

I would like to try and run it together with asyncio in one thread if possible. If it is not, or it is a bad idea, then I will try the threaded approach.

Thanks in advance for the help.

pthom commented 3 months ago

Hello,

As is common with GUI frameworks, the GUI needs to be in the main thread. I would advise you to use two separate threads, with asyncio in a separate thread.

Example:

import asyncio
import threading
from imgui_bundle import imgui, immapp, hello_imgui

#
# GUI code
#
def gui() -> None:
    imgui.text("Hello world")
    if imgui.button("Click me"):
        print("Button clicked")

def run_gui() -> None:
    runner_params = hello_imgui.RunnerParams()
    runner_params.callbacks.show_gui = gui
    immapp.run(runner_params)

#
# Asyncio code
#
async def other_async_task():
    while True:
        print("Async task running...")
        await asyncio.sleep(1)

def run_asyncio_loop():
    asyncio.run(other_async_task())

if __name__ == "__main__":
    # Start the asyncio loop in a separate thread
    asyncio_thread = threading.Thread(target=run_asyncio_loop)
    asyncio_thread.daemon = True  # Make sure it exits when the main program exits
    asyncio_thread.start()

    # Run the GUI on the main thread
    run_gui()
Willy-JL commented 2 months ago

i had this same requirement a while back and made a utility module async_thread.py that can be easily imported and reused to run multiple things in async thread, see below for source and usage example: https://gist.github.com/Willy-JL/183cb7134e940db1cfab72480e95a357

sidenote, just found imgui_bundle and it looks very nice! i will probably look at switching from pyimgui, served me well but sadly the maintainer doesnt have enough time to keep up with it and cant blame them, its a huge undertaking

bgribble commented 1 month ago

I am currently converting a Python GTK app using asyncio to ImGui. It's been a pretty smooth process.

I ended up using the SDL2Renderer from imgui_bundle.python_backends.sdl_backend. I think glfw is somewhat more popular but I couldn't get it to report keyboard and mouse events the way I wanted (my app does a lot of custom bindings).

In my app, I didn't create a separate thread for the UI, the main thread runs the asyncio event loop and the UI.

If you want to have a look here's the branch I am working on: bgribble/mfp ... the imgui code is in mfp/gui/imgui

pthom commented 1 month ago

@bgribble : thanks for the info. I'm always interested in seeing what users are doing with imgui bundle. I you have time could you post some screenshots of mfp in the gallery. Thanks