immapp_notebook.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Part of ImGui Bundle - MIT License - Copyright (c) 2022-2026 Pascal Thomet - https://github.com/pthom/imgui_bundle
  2. # mypy: disable_error_code=no-untyped-call
  3. from typing import Any, Callable, Tuple
  4. from imgui_bundle import immapp, hello_imgui
  5. GuiFunction = Callable[[], None]
  6. ScreenSize = Tuple[int, int]
  7. def _make_gui_with_light_theme(gui_function: GuiFunction) -> GuiFunction:
  8. @immapp.static(was_theme_set=False)
  9. def inner() -> None:
  10. static = inner
  11. if not static.was_theme_set:
  12. hello_imgui.apply_theme(hello_imgui.ImGuiTheme_.white_is_white)
  13. static.was_theme_set = True
  14. gui_function()
  15. return inner
  16. def _run_app_function_and_display_image_in_notebook(
  17. app_function: GuiFunction, # A function that runs the app entirely
  18. thumbnail_height: int = 0,
  19. thumbnail_ratio: float = 0.0,
  20. ) -> None:
  21. """ImguiBundle app runner for jupyter notebook
  22. This runner is able to:
  23. * run an ImGui app from a jupyter notebook
  24. * display a thumbnail of the app + a "run" button in the cell output
  25. The GUI will be rendered with a white theme.
  26. If window_size is left as its default value (0, 0), then the window will autosize.
  27. thumbnail_height and thumbnail_ratio control the size of the screenshot that is displayed after execution.
  28. """
  29. # pip install opencv-python or pip install opencv-contrib-python
  30. import cv2
  31. import PIL.Image # pip install pillow
  32. from IPython.display import display
  33. def make_thumbnail(image: Any) -> Any:
  34. resize_ratio = 1.0
  35. if thumbnail_height > 0:
  36. resize_ratio = thumbnail_height / image.shape[0]
  37. if thumbnail_ratio != 0.0:
  38. resize_ratio *= thumbnail_ratio
  39. if resize_ratio != 1:
  40. thumbnail_image = cv2.resize(
  41. image,
  42. (0, 0),
  43. fx=resize_ratio,
  44. fy=resize_ratio,
  45. interpolation=cv2.INTER_AREA,
  46. )
  47. else:
  48. thumbnail_image = image
  49. return thumbnail_image
  50. # def display_image(image):
  51. # pil_image = PIL.Image.fromarray(image)
  52. # display(pil_image)
  53. def display_image(image: Any) -> None:
  54. from IPython.display import Image
  55. # Convert the input image to a PIL image
  56. pil_image = PIL.Image.fromarray(image)
  57. # Save the PIL image to a bytes buffer in PNG format
  58. import io
  59. buffer = io.BytesIO()
  60. pil_image.save(buffer, format="JPEG")
  61. buffer.seek(0)
  62. # Create an IPython display Image object and specify PNG format
  63. jpeg_image = Image(data=buffer.getvalue(), format="jpeg")
  64. display(jpeg_image)
  65. def run_app_and_display_thumb() -> None:
  66. nonlocal thumbnail_ratio
  67. from imgui_bundle import hello_imgui
  68. app_function()
  69. app_image = hello_imgui.final_app_window_screenshot()
  70. scale = hello_imgui.final_app_window_screenshot_framebuffer_scale()
  71. if thumbnail_ratio == 0.0:
  72. thumbnail_ratio = 1.0 / scale
  73. thumbnail = make_thumbnail(app_image)
  74. display_image(thumbnail)
  75. run_app_and_display_thumb()