Skip to content

Graph Widget for Textual

Introduction

Netext has native support for textual and offers a widget to display and mutate graphs. The widget emits events for nodes and edges that have been clicked and also support the transformation from widget to view and graph coordinates.

Example

The following code is the most simple example. It uses the graph from the minimal example and shows it in a widget.

textual.py
from textual.app import App, ComposeResult
from textual.widgets import Header
from netext.textual.widget import GraphView

import networkx as nx

g = nx.Graph()
g.add_node("Hello")
g.add_node("World")
g.add_edge("Hello", "World")


class GraphviewApp(App):
    """A Textual app that displays a graph."""

    def compose(self) -> ComposeResult:
        """Create child widgets for the app."""
        yield Header()
        yield GraphView(g)


if __name__ == "__main__":
    app = GraphviewApp()
    app.run()

If you run this app you will see the following screen:

textual.py GraphviewApp ╭───────╮ Hello ╰───────╯ * * * ╭───────╮ World ╰───────╯