Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Immediate GUI

What is an Immediate GUI

Comparison between Immediate Mode GUI and Traditional GUI

An “Immediate Mode Graphical User Interface” lets you build user interfaces directly in code. This keeps the UI and app state in perfect sync with minimal boilerplate. This approach is especially popular for quick prototyping and tools because it’s intuitive, flexible, easy to maintain, and trivial to debug.

The example below shows a documented example to explain the Immediate Mode GUI paradigm:

Python
C++
from imgui_bundle import imgui, immapp

counter = 0 # our app state

# The gui() function is called every frame, so the UI updates in real time.
def gui():
    global counter

    # The state of the UI is always in sync with the app state,
    # via standard variables: debugging UI becomes trivial!
    imgui.text(f"Counter ={counter}")

    # We can display a button, and handle its action in one line:
    if imgui.button("increment counter"):
        counter += 1
    # Below, we can also set the counter value via a slider between 0 and 100
    value_changed, counter = imgui.slider_int("Set counter", counter, 0, 100)

# Run the app (in one line!)
immapp.run(gui)

It produces this simple app:

A screenshot of the app

Immediate Mode GUI does not mean that you cannot separate concerns!

You can still (and should) maintain a separate application state. The key difference is that your GUI can interact directly with that state in a straightforward way, without the need to maintain a separate UI state or complex event handling systems.

Dear ImGui

The most popular Immediate Mode GUI library is Dear ImGui, a powerful C++ library originally created for real-time tools in game engines, now widely used in many industries, with over 60k stars on GitHub.

Dear ImGui Bundle includes Dear ImGui plus many extra libraries, making it ideal for rapid prototyping as well as building complex apps with advanced widgets, plotting, node editors; in C++ and Python.

Get started in no time with Hello ImGui and ImmApp

With Hello ImGui and ImmApp (both included in Dear ImGui Bundle), you can create a full-featured GUI application with just a few lines of code.

Hello World in 4 lines

4 lines are enough to start a GUI application!

Python
C++
from imgui_bundle import imgui, immapp

def gui():
    imgui.text("Hello, world!")
immapp.run(gui)

A more complete example with plots

The example below shows how to create a more complete application that uses an add-on (ImPlot) for plotting data.

Python
C++
import time
import numpy as np

from imgui_bundle import implot, imgui, immapp, imgui_knobs

# Fill x and y whose plot is a heart
vals = np.arange(0, np.pi * 2, 0.01)
x = np.power(np.sin(vals), 3) * 16
y = 13 * np.cos(vals) - 5 * np.cos(2 * vals) - 2 * np.cos(3 * vals) - np.cos(4 * vals)
# Heart pulse rate and time tracking
phase = 0.0
t0 = time.time() + 0.2
heart_pulse_rate = 80


def gui():
    global heart_pulse_rate, phase, t0, x, y

    # Change heart size over time, according to the pulse rate
    t = time.time()
    phase += (t - t0) * heart_pulse_rate / (np.pi * 2)
    k = 0.8 + 0.1 * np.cos(phase)
    t0 = t

    # Plot the heart
    if implot.begin_plot("Heart", immapp.em_to_vec2(21, 21)):
        implot.plot_line("", x * k, y * k)
        implot.end_plot()

    # let the user set the pulse rate via a knob
    _, heart_pulse_rate = imgui_knobs.knob("Pulse Rate", heart_pulse_rate, 30.0, 180.0)


if __name__ == "__main__":
    immapp.run(gui,
               window_size_auto=True,
               window_title="Hello!",
               with_implot=True,
               fps_idle=0  # Make sure that the animation is smooth (do not limit fps when idle)
               )
heart

Quickly deploy your apps on the web

These apps can be easily deployed on the web, either in C++ via Emscripten, or in Python via Pyodide.