Giter Site home page Giter Site logo

Graph rendering about aiogram_dialog HOT 2 CLOSED

Tishka17 avatar Tishka17 commented on June 12, 2024
Graph rendering

from aiogram_dialog.

Comments (2)

Tishka17 avatar Tishka17 commented on June 12, 2024

TODO:

  1. Next/Prev/etc transitions set as callbacks.
  2. Transitions which set in code. (AST?)

from aiogram_dialog.

Tishka17 avatar Tishka17 commented on June 12, 2024

Using diagram:

from typing import List, Sequence, Tuple

from aiogram.dispatcher.filters.state import State
from diagrams import Diagram, Cluster, Edge
from diagrams.generic.device import Tablet

from aiogram_dialog import Dialog
from aiogram_dialog.widgets.kbd import Group, Back, Cancel, Start, SwitchTo, Next


def walk_keyboard(nodes, dialog, starts: List[Tuple[State, State]], current_state: State, keyboards: Sequence):
    for kbd in keyboards:
        if isinstance(kbd, Group):
            walk_keyboard(nodes, dialog, starts, current_state, kbd.buttons)
        elif isinstance(kbd, Start):
            nodes[current_state] >> Edge(color="#338a3e") >> nodes[kbd.state]
        elif isinstance(kbd, SwitchTo):
            nodes[current_state] >> Edge() >> nodes[kbd.state]
        elif isinstance(kbd, Next):
            new_state = dialog.states[dialog.states.index(current_state) + 1]
            nodes[current_state] >> Edge() >> nodes[new_state]
        elif isinstance(kbd, Back):
            new_state = dialog.states[dialog.states.index(current_state) - 1]
            nodes[current_state] >> Edge(color="grey") >> nodes[new_state]
        elif isinstance(kbd, Cancel):
            for from_, to_ in starts:
                if to_.group == current_state.group:
                    nodes[current_state] >> Edge(color="grey", style="dashed") >> nodes[from_]


def find_starts(current_state, keyboards: Sequence):
    for kbd in keyboards:
        if isinstance(kbd, Group):
            yield from find_starts(current_state, kbd.buttons)
        elif isinstance(kbd, Start):
            yield current_state, kbd.state


def render(dialogs: List[Dialog]):
    with Diagram('Aiogram Dialog', outformat="png", show=False):
        nodes = {}
        for dialog in dialogs:
            with Cluster(dialog.states_group_name()):
                for window in dialog.windows.values():
                    nodes[window.get_state()] = Tablet(label=window.get_state().state)

        starts = []
        for dialog in dialogs:
            for window in dialog.windows.values():
                starts.extend(find_starts(window.get_state(), [window.keyboard]))

        for dialog in dialogs:
            for window in dialog.windows.values():
                walk_keyboard(nodes, dialog, starts, window.get_state(), [window.keyboard])


if __name__ == '__main__':
    render([
        # here put you dialogs
    ])

from aiogram_dialog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.