{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exploring `doit` graphs with `ipywidgets`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as W, traitlets as T, doit, graphviz, jinja2, textwrap, re; ui = W.VBox([W.HTML('restart and run all to see something here')]); ui" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Doit(W.Widget):\n", " doit_ = T.Instance(doit.doit_cmd.DoitMain)\n", " tasks = W.trait_types.TypedTuple(trait=T.Instance(doit.task.Task))\n", " \n", " @T.observe(\"doit_\")\n", " def on_doit_change(self, change):\n", " cmds = self.doit_.get_cmds()\n", " tasks, _ = self.doit_.task_loader.load_tasks(cmds['list'], None, None)\n", " tc = doit.control.TaskControl(tasks)\n", " self.tasks = tuple(tc.tasks.values())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class FileDoit(Doit):\n", " path = T.Unicode(\"dodo.py\").tag(sync=True)\n", " \n", " def __init__(self, *args, **kwargs):\n", " super().__init__(*args, **kwargs)\n", " self.on_path_change(T.Bunch(new=self.path))\n", " \n", " @T.observe(\"path\")\n", " def _on_path_change(self, change):\n", " self.on_path_change(change) \n", " \n", " def on_path_change(self, change):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class PyFileDoit(FileDoit): \n", " def on_path_change(self, change=None):\n", " self.doit_ = doit.doit_cmd.DoitMain(doit.cmd_base.ModuleTaskLoader(doit.loader.get_module(self.path)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "it = PyFileDoit(path = \"dodo.py\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class TaskGraph(W.HTML):\n", " tasks = W.trait_types.TypedTuple(trait=T.Instance(doit.task.Task))\n", " graph = T.Instance(graphviz.Digraph)\n", " node_template = T.Unicode(\"\"\"<\n", "
{{t.name}}
{{wrap(t.doc)}}
\n", " >\"\"\")\n", " filter_text = T.Unicode()\n", " filter_kind = T.Unicode()\n", " rankdir = T.Unicode(\"RL\")\n", " wrap = T.Int(24)\n", " \n", " @T.observe(\"tasks\", \"node_template\", \"filter_text\", \"filter_kind\", \"wrap\", \"rankdir\")\n", " def on_tasks(self, change):\n", " tmpl = jinja2.Template(self.node_template)\n", " wrap = lambda x: \"
\".join(textwrap.wrap(x, self.wrap))\n", " self.graph = G = graphviz.Digraph(format=\"svg\", node_attr=dict(fontname=\"sans-serif\"))\n", " G.attr(rankdir=self.rankdir)\n", " tasks = self.filtered()\n", " task_names = [t.name for t in tasks]\n", " for t in tasks:\n", " G.node(t.name, tmpl.render(t=t, wrap=wrap), shape=\"none\")\n", " for tdep in t.task_dep:\n", " if tdep in task_names:\n", " G.edge(tdep, t.name)\n", " \n", " self.value = re.sub(r'