|
| 1 | +## Draw a graph of module inter-dependencies with [pydeps](https://github.com/thebjorn/pydeps) |
| 2 | + |
| 3 | +```python exec="true" show_source="tabbed-right" isolate="yes" |
| 4 | +from pydeps import cli, colors, py2depgraph, dot |
| 5 | +from pydeps.pydeps import depgraph_to_dotsrc |
| 6 | +from pydeps.target import Target |
| 7 | + |
| 8 | +cli.verbose = cli._not_verbose |
| 9 | +options = cli.parse_args(["src/markdown_exec", "--noshow"]) |
| 10 | +colors.START_COLOR = options["start_color"] |
| 11 | +target = Target(options["fname"]) |
| 12 | +with target.chdir_work(): |
| 13 | + dep_graph = py2depgraph.py2dep(target, **options) |
| 14 | +dot_src = depgraph_to_dotsrc(target, dep_graph, **options) |
| 15 | +svg = dot.call_graphviz_dot(dot_src, "svg").decode() |
| 16 | +svg = "".join(svg.splitlines()[6:]) |
| 17 | +svg = svg.replace('fill="white"', 'fill="transparent"') |
| 18 | +modules_map = { |
| 19 | + "markdown_exec": "../reference/markdown_exec/", |
| 20 | + "markdown_exec_python": "../reference/markdown_exec/python/", |
| 21 | + "markdown_exec_utils": "../reference/markdown_exec/utils/", |
| 22 | +} |
| 23 | +for title, href in modules_map.items(): |
| 24 | + title_tag = f"<title>{title}</title>" |
| 25 | + svg = svg.replace(title_tag, f'<a href="{href}">' + title_tag) |
| 26 | +svg = svg.replace("</text></g>", "</text></a></g>") |
| 27 | +output_html(svg) |
| 28 | +``` |
| 29 | + |
| 30 | +## Run a Python module and print its output |
| 31 | + |
| 32 | +```python exec="true" show_source="tabbed-right" isolate="yes" |
| 33 | +import argparse |
| 34 | +import sys |
| 35 | +import warnings |
| 36 | +from contextlib import suppress |
| 37 | +from io import StringIO |
| 38 | +from runpy import run_module |
| 39 | + |
| 40 | +old_argv = list(sys.argv) |
| 41 | +sys.argv = ["mkdocs"] |
| 42 | +old_stdout = sys.stdout |
| 43 | +sys.stdout = StringIO() |
| 44 | +warnings.filterwarnings("ignore", category=RuntimeWarning) |
| 45 | +with suppress(SystemExit): |
| 46 | + run_module("mkdocs", run_name="__main__") |
| 47 | +output = sys.stdout.getvalue() |
| 48 | +sys.stdout = old_stdout |
| 49 | +sys.argv = old_argv |
| 50 | + |
| 51 | +output_markdown(f"```\n{output}\n```") |
| 52 | +``` |
| 53 | + |
| 54 | +## Format help of a CLI tool based on argparse in a code block |
| 55 | + |
| 56 | +```python exec="true" show_source="tabbed-right" isolate="yes" |
| 57 | +from duty.cli import get_parser |
| 58 | +parser = get_parser() |
| 59 | +output_markdown(f"```\n{parser.format_help()}\n```") |
| 60 | +``` |
| 61 | + |
| 62 | +## Format help of a CLI tool based on argparse as Markdown |
| 63 | + |
| 64 | +```python exec="true" show_source="tabbed-right" isolate="yes" |
| 65 | +import argparse |
| 66 | +from duty.cli import get_parser |
| 67 | +parser = get_parser() |
| 68 | +lines = [] |
| 69 | +lines.append(f"## duty") |
| 70 | +if parser.description: |
| 71 | + lines.append(parser.description) |
| 72 | +lines.append("\nOptions:\n") |
| 73 | +for action in parser._actions: |
| 74 | + opts = [f"`{opt}`" for opt in action.option_strings] |
| 75 | + if not opts: |
| 76 | + continue |
| 77 | + line = "- " + ",".join(opts) |
| 78 | + if action.metavar: |
| 79 | + line += f" `{action.metavar}`" |
| 80 | + line += f": {action.help}" |
| 81 | + if action.default and action.default != argparse.SUPPRESS: |
| 82 | + line += f"(default: {action.default})" |
| 83 | + lines.append(line) |
| 84 | +output_markdown("\n".join(lines)) |
| 85 | +``` |
| 86 | + |
| 87 | +## Draw a diagram using the Diagrams library |
| 88 | + |
| 89 | +```python exec="true" show_source="tabbed-right" |
| 90 | +from base64 import b64encode |
| 91 | +from contextlib import suppress |
| 92 | +from diagrams import Diagram, setdiagram |
| 93 | +from diagrams.k8s.clusterconfig import HPA |
| 94 | +from diagrams.k8s.compute import Deployment, Pod, ReplicaSet |
| 95 | +from diagrams.k8s.network import Ingress, Service |
| 96 | + |
| 97 | +with suppress(FileNotFoundError): |
| 98 | + with Diagram("Exposed Pod with 3 Replicas", show=False) as diagram: |
| 99 | + diagram.render = lambda: None |
| 100 | + net = Ingress("domain.com") >> Service("svc") |
| 101 | + net >> [Pod("pod1"), |
| 102 | + Pod("pod2"), |
| 103 | + Pod("pod3")] << ReplicaSet("rs") << Deployment("dp") << HPA("hpa") |
| 104 | + png = b64encode(diagram.dot.pipe(format="png")).decode() |
| 105 | + |
| 106 | +output_html(f'<img src="data:image/png;base64, {png}"/>') |
| 107 | +``` |
0 commit comments