Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

diagram.md 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ---
  2. id: diagram
  3. title: Diagrams
  4. ---
  5. `Diagram` is a primary object representing a diagram.
  6. ## Basic
  7. `Diagram` represents a global diagram context.
  8. You can create a diagram context with the `Diagram` class. The first parameter of the `Diagram` constructor will be used to generate the output filename.
  9. ```python
  10. from diagrams import Diagram
  11. from diagrams.aws.compute import EC2
  12. with Diagram("Simple Diagram"):
  13. EC2("web")
  14. ```
  15. If you run the above script with the command below,
  16. ```shell
  17. $ python diagram.py
  18. ```
  19. it will generate an image file with single `EC2` node drawn as `simple_diagram.png` in your working directory and open that created image file immediately.
  20. ## Jupyter Notebooks
  21. Diagrams can also be rendered directly inside Jupyter notebooks like this:
  22. ```python
  23. from diagrams import Diagram
  24. from diagrams.aws.compute import EC2
  25. with Diagram("Simple Diagram") as diag:
  26. EC2("web")
  27. diag
  28. ```
  29. ## Options
  30. You can specify the output file format with the `outformat` parameter. The default is **png**.
  31. > Allowed formats are: png, jpg, svg, pdf, and dot
  32. ```python
  33. from diagrams import Diagram
  34. from diagrams.aws.compute import EC2
  35. with Diagram("Simple Diagram", outformat="jpg"):
  36. EC2("web")
  37. ```
  38. The `outformat` parameter also supports a list to output all the defined outputs in one call:
  39. ```python
  40. from diagrams import Diagram
  41. from diagrams.aws.compute import EC2
  42. with Diagram("Simple Diagram Multi Output", outformat=["jpg", "png", "dot"]):
  43. EC2("web")
  44. ```
  45. You can specify the output filename with the `filename` parameter. The extension shouldn't be included, it's determined by the `outformat` parameter.
  46. ```python
  47. from diagrams import Diagram
  48. from diagrams.aws.compute import EC2
  49. with Diagram("Simple Diagram", filename="my_diagram"):
  50. EC2("web")
  51. ```
  52. You can also disable the automatic file opening by setting the `show` parameter to **false**. The default is **true**.
  53. ```python
  54. from diagrams import Diagram
  55. from diagrams.aws.compute import EC2
  56. with Diagram("Simple Diagram", show=False):
  57. EC2("web")
  58. ```
  59. Diagrams also allow custom Graphviz dot attributes options.
  60. > `graph_attr`, `node_attr` and `edge_attr` are supported. Here is a [reference link](https://www.graphviz.org/doc/info/attrs.html).
  61. ```python
  62. from diagrams import Diagram
  63. from diagrams.aws.compute import EC2
  64. graph_attr = {
  65. "fontsize": "45",
  66. "bgcolor": "transparent"
  67. }
  68. with Diagram("Simple Diagram", show=False, graph_attr=graph_attr):
  69. EC2("web")
  70. ```