diff --git a/README.md b/README.md index ddf7c4c..310c251 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ All additional options are listed below: | `-nt`
`--no-tikz` | Force to not import the TikZ package. | | `-l=`
`--tikz-libraries=` | Comma-separated list of TikZ libraries.
    *Example:* `-l=calc,arrows`.
    *Defaults* to None. | | `-lp=`
`--pgfplots-libraries=` | Comma-separated list of pgfplots libraries.
    *Example:* `-pl=groupplots,external`.
    *Defaults* to None. | -| `-j`
`--use-jinja` | Render the code using Jinja2. | +| `-dj`
`--disable-jinja` | Disable Jinja2 rendering. | | `-pj`
`--print-jinja` | Print the rendered Jinja2 template. | | `-pt`
`--print-tex` | Print the full LaTeX document. | | `-sc=`
`--scale=` | The scale factor to apply to the TikZ diagram.
    *Example:* `-sc=0.5`.
    *Defaults* to `-sc=1.0`. | @@ -257,6 +257,15 @@ Contributions are welcome from everyone! Whether you're reporting bugs, submitti All notable changes to this project are presented below. +## v0.5 + +**๐Ÿšจ Breaking Changes** + +- Significant changes to Jinja2 rendering: + - Replaced the default Jinja2 syntax with a custom one to avoid clashes with LaTeX braces (`{}`). + - With the new syntax, conflicts with LaTeX are significantly reduced, thus Jinja2 is now enabled by default and has become a mandatory dependency. + - Added a `--disable-jinja` flag to allow optional disabling of Jinja2 rendering. + ## v0.4.2 **๐Ÿž Bug Fixes** @@ -349,7 +358,7 @@ All notable changes to this project are presented below. # Thanks -I had been using [ITikZ](https://github.com/jbn/itikz) for years. However, it doesn't update often and relies on the outdated `pdf2svg` to convert PDFs to images, which causes problems in Windows environments. Inspired by ITikZ and [IPython TikZ Magic](https://github.com/mkrphys/ipython-tikzmagic), I decided to create my own package, adding new features such as the ability to work with preambles and save the LaTeX result to IPython variables. I also switched from `pdf2svg` to Poppler, which works perfectly in Windows. +I had been using [ITikZ](https://github.com/jbn/itikz) for years. However, it doesn't update often and relies on the outdated `pdf2svg` for converting PDFs to images, which causes problems in Windows environments. Inspired by ITikZ and [IPython TikZ Magic](https://github.com/mkrphys/ipython-tikzmagic), I decided to create my own package, adding new features such as support for preambles, new Jinja syntax, and the ability to save the LaTeX result to IPython variables. I also switched from `pdf2svg` to Poppler, which works perfectly on all plataforms, including Windows. # License diff --git a/docs/about/changelog.md b/docs/about/changelog.md index 3a5e588..94e8dae 100644 --- a/docs/about/changelog.md +++ b/docs/about/changelog.md @@ -1,5 +1,14 @@ All notable changes to this project are presented below. +## v0.5 + +**๐Ÿšจ Breaking Changes** + +- Significant changes to Jinja2 rendering: + - Replaced the default Jinja2 syntax with a custom one to avoid clashes with LaTeX braces (`{}`). + - With the new syntax, conflicts with LaTeX are significantly reduced, thus Jinja2 is now enabled by default and has become a mandatory dependency. + - Added a `--disable-jinja` flag to allow optional disabling of Jinja2 rendering. + ## v0.4.2 **๐Ÿž Bug Fixes** diff --git a/docs/assets/tikz/disable_jinja.svg b/docs/assets/tikz/disable_jinja.svg new file mode 100644 index 0000000..a1138c0 --- /dev/null +++ b/docs/assets/tikz/disable_jinja.svg @@ -0,0 +1,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/templates/motivation.md b/docs/templates/motivation.md index 0bf8aeb..cdea33d 100644 --- a/docs/templates/motivation.md +++ b/docs/templates/motivation.md @@ -1 +1 @@ -I had been using [ITikZ](https://github.com/jbn/itikz) for years. However, it doesn't update often and relies on the outdated `pdf2svg` to convert PDFs to images, which causes problems in Windows environments. Inspired by ITikZ and [IPython TikZ Magic](https://github.com/mkrphys/ipython-tikzmagic), I decided to create my own package, adding new features such as the ability to work with preambles and save the LaTeX result to IPython variables. I also switched from `pdf2svg` to Poppler, which works perfectly in Windows. \ No newline at end of file +I had been using [ITikZ](https://github.com/jbn/itikz) for years. However, it doesn't update often and relies on the outdated `pdf2svg` for converting PDFs to images, which causes problems in Windows environments. Inspired by ITikZ and [IPython TikZ Magic](https://github.com/mkrphys/ipython-tikzmagic), I decided to create my own package, adding new features such as support for preambles, new Jinja syntax, and the ability to save the LaTeX result to IPython variables. I also switched from `pdf2svg` to Poppler, which works perfectly on all plataforms, including Windows. \ No newline at end of file diff --git a/docs/usage/as-magic.md b/docs/usage/as-magic.md index a582910..d51c7f7 100644 --- a/docs/usage/as-magic.md +++ b/docs/usage/as-magic.md @@ -34,11 +34,11 @@ Finally, if you forget the usage, as for help by typing `%tikz?`, or visit **add
 %tikz [-as INPUT_TYPE] [-i] [-f] [-p LATEX_PREAMBLE] [-t TEX_PACKAGES]
-        [-nt] [-l TIKZ_LIBRARIES] [-lp PGFPLOTS_LIBRARIES] [-j] [-pj]
-        [-pt] [-sc SCALE] [-r] [-d DPI] [-e] [-tp TEX_PROGRAM]
-        [-ta TEX_ARGS] [-nc] [-s SAVE_TEX] [-S SAVE_IMAGE] [-sv SAVE_VAR]
-        [--as-jinja]
-        [code]
+            [-nt] [-l TIKZ_LIBRARIES] [-lp PGFPLOTS_LIBRARIES] [-dj] [-pj]
+            [-pt] [-sc SCALE] [-r] [-d DPI] [-g] [-e] [-k] [-tp TEX_PROGRAM]
+            [-ta TEX_ARGS] [-nc] [-s SAVE_TIKZ] [-st SAVE_TEX] [-sp SAVE_PDF]
+            [-S SAVE_IMAGE] [-sv SAVE_VAR]
+            [code]
 
 Renders a TikZ diagram in a Jupyter notebook cell. This function can be used as both a line magic (%tikz) and a cell magic (%%tikz).
 
@@ -66,7 +66,6 @@ Additional options can be passed to the magic command to customize LaTeX code an
            ...:     (m-1-2) edge node [right] {$bd$} (m-2-2)
            ...:     (m-2-1) edge node [below] {$cd$} (m-2-2);
 
-
 positional arguments:
   code                  the variable in IPython with the Tex/TikZ code
 
@@ -92,7 +91,7 @@ options:
   -lp PGFPLOTS_LIBRARIES, --pgfplots-libraries PGFPLOTS_LIBRARIES
                         Comma-separated list of pgfplots libraries, e.g.,
                         `-pl=groupplots,external`.
-  -j, --use-jinja       Render the code using Jinja2.
+  -dj, --disable-jinja  Disable Jinja2 rendering.
   -pj, --print-jinja    Print the rendered Jinja2 template.
   -pt, --print-tex      Print the full LaTeX document.
   -sc SCALE, --scale SCALE
@@ -101,9 +100,9 @@ options:
   -r, --rasterize       Output a rasterized image (PNG) instead of SVG.
   -d DPI, --dpi DPI     DPI to use when rasterizing the image, e.g.,
                         `--dpi=300`. Defaults to `-d=96`.
-  -g, --gray            Set grayscale to a rasterized image.
+  -g, --gray            Set grayscale to the rasterized image.
   -e, --full-err        Print the full error message when an error occurs.
-  -k, --keep-temp       Keep temporary LaTeX files.
+  -k, --keep-temp       Keep temporary files.
   -tp TEX_PROGRAM, --tex-program TEX_PROGRAM
                         TeX program to use for compilation, e.g.,
                         `-tp=xelatex` or `-tp=lualatex`. Defaults to
@@ -125,7 +124,6 @@ options:
   -sv SAVE_VAR, --save-var SAVE_VAR
                         Save the TikZ or LaTeX code to an IPython variable,
                         e.g., `-sv my_var`.
-  --as-jinja            Deprecated. Use `--use-jinja` instead.
 
@@ -658,12 +656,6 @@ conway_str = r"""\documentclass[tikz]{standalone} To help ensure that TikZ Pictures stay aligned with your data, you can use [Jinja2 templates](https://jinja.palletsprojects.com/en/latest/templates/). -!!! warning - Before using it, you must install Jinja2: - ```shell - pip install jinja2 - ``` - First, we need to populate some data: ```python @@ -677,25 +669,36 @@ nodes {"'A': 0, 'B': 60, 'C': 121, 'D': 182, 'E': 243, 'F': 304} -Then, you can interpret the cell source as a Jinja2 template using the `-j` (or `--use-jinja`) parameter: +Since [version 0.5](https://jupyter-tikz.readthedocs.io/stable/about/changelog/), we have modified the standard Jinja2 syntax because `{}` braces clash with LaTeX. The table below shows the differences between the standard Jinja2 syntax and the Jupyter TikZ Jinja syntax: + +| Standard Jinja2 Syntax | Jupyter TikZ Syntax | Example | +| :--------------------: | :-------------------------: | :--------------------------: | +| `{{ expression }}` | `(** expression **)` | `(** for n1 in range(n) **)` | +| `{% logic/block %}` | `(* logic/block *)` | `\node at((* angle *):1);` | +| `{# comment #}` | `(~ comment ~)` | `(~ This wonโ€™t render ~)` | + +!!! tip + Since [version 0.5](https://jupyter-tikz.readthedocs.io/stable/about/changelog/), Jinja2 templates are enabled by default, so it's no longer necessary to use the `-j` flag. The `jinja2` package is automatically installed during the installation of `jupyter_tikz`. + ```latex -%%tikz -j -l=arrows,automata -sc=2 +%%tikz -l=arrows,automata -sc=2 {% raw %} \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick] \tikzstyle{every state}=[fill=mymagenta,draw=none,text=white] - - {% for name, angle in nodes.items() -%} - \node[color=magenta] (v{{ loop.index0 }}) at ({{ angle }}:1) {${{ name }}$}; - {% endfor -%} - - {% for n1 in range(n) -%} - {% for n2 in range(n) -%} - {%if n1 < n2 -%} - \path (v{{n1}}) edge (v{{n2}}); - {% endif -%} - {% endfor -%} - {% endfor -%} + + % Nodes + (** for name, angle in nodes.items() **)(~ For expression ~) + \node[color=magenta] (v(* loop.index0 *)) at ((* angle *):1) {$(* name *)$}; + (** endfor **) + % Paths + (** for n1 in range(n) **) + (** for n2 in range(n) **) + (** if n1 < n2 **) + \path (v(* n1 *)) edge (v(* n2 *)); + (** endif **) + (** endfor **) + (** endfor **) \end{tikzpicture} {% endraw %} ``` @@ -707,7 +710,7 @@ Then, you can interpret the cell source as a Jinja2 template using the `-j` (or It also works for full documents and implicit pictures: ```latex -%%tikz --use-jinja -as=f -r -d=200 +%%tikz -as=f -r -d=200 {% raw %} \documentclass[tikz]{standalone} \usetikzlibrary{arrows,automata} @@ -716,18 +719,18 @@ It also works for full documents and implicit pictures: \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick] \tikzstyle{every state}=[fill=mymagenta,draw=none,text=white] - - {% for name, angle in nodes.items() -%} - \node[color=mymagenta] (v{{loop.index0}}) at ({{angle}}:1) {${{name}}$}; - {% endfor -%} - - {% for n1 in range(n) -%} - {% for n2 in range(n) -%} - {%if n1 < n2 -%} - \path (v{{n1}}) edge (v{{n2}}); - {% endif -%} - {% endfor -%} - {% endfor -%} + + (** for name, angle in nodes.items() **) + \node[color=mymagenta] (v(* loop.index0 *)) at((* angle *):1) {$(* name *)$}; + (** endfor **) + + (** for n1 in range(n) **) + (** for n2 in range(n) **) + (** if n1 < n2 **) + \path (v(* n1 *)) edge (v(* n2 *)); + (** endif **) + (** endfor **) + (** endfor **) \end{tikzpicture} \end{document} {% endraw %} @@ -738,29 +741,32 @@ It also works for full documents and implicit pictures: ### Print Jinja -Sometimes, you'll make mistakes. Debugging transpiled code is challenging, especially without a mapping. To assist, you can print the Jinja template using `-pj` (or `--print-jinja`)(1): +Sometimes, you'll make mistakes. Debugging transpiled code is challenging, especially without a mapping. To assist, you can print the Jinja rendered output using the `-pj` (or `--print-jinja`) flag (1): { .annotate } -1. The saved code file is also the interpolated code source. +1. The `-pj` printed code is also the source of the interpolated code. + +!!! tip + Use a minus (`-`) before and/or after a block for whitespace control. For more information, refer to the [Jinja2 documentation](https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control). ```latex %%tikz -pj -l=arrows,automata -sc=2 --save-tex=outputs/jinja_rendered.tikz {% raw %} \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick] \tikzstyle{every state}=[fill=mymagenta,draw=none,text=white] - - {% for name, angle in nodes.items() -%} - \node[color=magenta] (v{{ loop.index0 }}) at ({{ angle }}:1) {${{ name }}$}; - {% endfor -%} - - {% for n1 in range(n) -%} - {% for n2 in range(n) -%} - {%if n1 < n2 -%} - \path (v{{n1}}) edge (v{{n2}}); - {% endif -%} - {% endfor -%} - {% endfor -%} + (~ Using minus `-` before/after each block to whitespace control. -~) + (~- https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control -~) + (** for name, angle in nodes.items() -**) + \node[color=magenta] (v(* loop.index0 *)) at((* angle *):1) {$(* name *)$}; + (** endfor **) + (** for n1 in range(n) -**) + (** for n2 in range(n) -**) + (**- if n1 < n2 -**) + \path (v(* n1 *)) edge (v(* n2 *)); + (** endif -**) + (** endfor -**) +(** endfor **) \end{tikzpicture} {% endraw %} ``` @@ -769,33 +775,48 @@ Sometimes, you'll make mistakes. Debugging transpiled code is challenging, espec
 \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick]
   \tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]
+
+  \node[color=magenta] (v0) at(0:1) {$A$};
+  \node[color=magenta] (v1) at(60:1) {$B$};
+  \node[color=magenta] (v2) at(121:1) {$C$};
+  \node[color=magenta] (v3) at(182:1) {$D$};
+  \node[color=magenta] (v4) at(243:1) {$E$};
+  \node[color=magenta] (v5) at(304:1) {$F$};
   
-  \node[color=magenta] (v0) at (0:1) {$A$};
-  \node[color=magenta] (v1) at (60:1) {$B$};
-  \node[color=magenta] (v2) at (121:1) {$C$};
-  \node[color=magenta] (v3) at (182:1) {$D$};
-  \node[color=magenta] (v4) at (243:1) {$E$};
-  \node[color=magenta] (v5) at (304:1) {$F$};
   \path (v0) edge (v1);
-         \path (v0) edge (v2);
-         \path (v0) edge (v3);
-         \path (v0) edge (v4);
-         \path (v0) edge (v5);
-         \path (v1) edge (v2);
-         \path (v1) edge (v3);
-         \path (v1) edge (v4);
-         \path (v1) edge (v5);
-         \path (v2) edge (v3);
-         \path (v2) edge (v4);
-         \path (v2) edge (v5);
-         \path (v3) edge (v4);
-         \path (v3) edge (v5);
-         \path (v4) edge (v5);
-         \end{tikzpicture}
+  \path (v0) edge (v2);
+  \path (v0) edge (v3);
+  \path (v0) edge (v4);
+  \path (v0) edge (v5);
+  \path (v1) edge (v2);
+  \path (v1) edge (v3);
+  \path (v1) edge (v4);
+  \path (v1) edge (v5);
+  \path (v2) edge (v3);
+  \path (v2) edge (v4);
+  \path (v2) edge (v5);
+  \path (v3) edge (v4);
+  \path (v3) edge (v5);
+  \path (v4) edge (v5);
+  
+\end{tikzpicture}
 
![Using Jinja](../assets/tikz/graph_letters.svg) +### Disabling Jinja rendering + +If you don't want to use Jinja2 rendering you can tell it using the flag `-dj` (or `--disable-jinja`): +```latex +%%tikz -dj -sc=2 +\begin{tikzpicture} + \node {(* Show `(*` because i'm not rendering Jinja*)}; +\end{tikzpicture} +``` +
+![Disable jinja](../assets/tikz/disable_jinja.svg) +
+ ## Exporting code to variables With the flag `-sv=`, it is possible to save the code to an IPython string variable(1). @@ -820,7 +841,7 @@ Now, we can reuse the variable in the code with Jinja: %%tikz -as=t --use-jinja {% raw %} \begin{tikzpicture}[scale=3] - {{ my_frame }} % This is my_frame that I rendered before + (* my_frame -*) % This is my_frame that I rendered before \filldraw (0.5,0.5) circle (.1); \end{tikzpicture} {% endraw %} @@ -832,14 +853,14 @@ Now, we can reuse the variable in the code with Jinja: ### Combining with Jinja2 -You can also combine `-sv` with `-j`: +You can also combine `-sv` with Jinja blocks: ```latex -%%tikz -as=t -j -sv=node_names -sc=2 +%%tikz -as=t -sv=node_names -sc=2 {% raw %} -{% for name, angle in nodes.items() -%} - \node[color=red] (v{{loop.index0}}) at ({{angle}}:1) {${{name}}$}; -{% endfor -%} +(** for name, angle in nodes.items() -**) + \node[color=red] (v(* loop.index0 *)) at((* angle *):1) {$(* name *)$}; +(** endfor -**) {% endraw %} ```
@@ -853,27 +874,26 @@ node_names ```
-'\\node[color=red] (v0) at (0:1) {$A$};\n\\node[color=red] (v1) at (60:1) {$B$};\n\\node[color=red] (v2) at (121:1) {$C$};\n\\node[color=red] (v3) at (182:1) {$D$};\n\\node[color=red] (v4) at (243:1) {$E$};\n\\node[color=red] (v5) at (304:1) {$F$};\n' +'\\node[color=red] (v0) at(0:1) {$A$};\n\\node[color=red] (v1) at(60:1) {$B$};\n\\node[color=red] (v2) at(121:1) {$C$};\n\\node[color=red] (v3) at(182:1) {$D$};\n\\node[color=red] (v4) at(243:1) {$E$};\n\\node[color=red] (v5) at(304:1) {$F$};\n'
Now, incorporate it into another `tikzpicture`: ```latex -%%tikz --use-jinja -l=arrows,automata -sc=2 +%%tikz -l=arrows,automata -sc=2 {% raw %} \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick] \tikzstyle{every state}=[fill=mymagenta,draw=none,text=white] - - {{ node_names }} - - {% for n1 in range(n) -%} - {% for n2 in range(n) -%} - {%if n1 < n2 -%} - \path (v{{n1}}) edge (v{{n2}}); - {% endif -%} - {% endfor -%} - {% endfor -%} + (* node_names *) + + (** for n1 in range(n) -**) + (** for n2 in range(n) -**) + (** if n1 < n2 -**) + \path (v(* n1 *)) edge (v(* n2 *)); + (** endif **) + (** endfor **) + (** endfor **) \end{tikzpicture} {% endraw %} ``` diff --git a/docs/usage/as-package.md b/docs/usage/as-package.md index 6ea57c5..15a1cce 100644 --- a/docs/usage/as-package.md +++ b/docs/usage/as-package.md @@ -102,6 +102,8 @@ The output image and code will be saved in: ## Working with Jinja +You can render Jinja by simply creating an object with Jinja code. + Firstly, let's create a Jinja template: ```python @@ -130,13 +132,13 @@ jinja_code = r"""\documentclass[tikz]{standalone} {% endraw %} ``` -You can render Jinja by passing the `use-jinja` option(1): +Then, pass the `jinja_code` into the creation of the `TexDocument` (1): { .annotate } -1. If you are using Jinja, you must pass the `ns=` parameter in order to allow the method to access the variables. +1. You must pass the `ns=` parameter in other to allow the method to access the variable. ```python -tex_jinja_document = TexDocument(jinja_code, use_jinja=True, ns=locals()) +tex_jinja_document = TexDocument(jinja_code, ns=locals()) print(tex_jinja_document) # It prints the rendered Jinja Code ```
@@ -148,29 +150,32 @@ print(tex_jinja_document) # It prints the rendered Jinja Code \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick] \tikzstyle{every state}=[fill=mymagenta,draw=none,text=white] + + \node[color=mymagenta] (v0) at(0:1) {$A$}; + \node[color=mymagenta] (v1) at(60:1) {$B$}; + \node[color=mymagenta] (v2) at(121:1) {$C$}; + \node[color=mymagenta] (v3) at(182:1) {$D$}; + \node[color=mymagenta] (v4) at(243:1) {$E$}; + \node[color=mymagenta] (v5) at(304:1) {$F$}; - \node[color=mymagenta] (v0) at (0:1) {$A$}; - \node[color=mymagenta] (v1) at (60:1) {$B$}; - \node[color=mymagenta] (v2) at (121:1) {$C$}; - \node[color=mymagenta] (v3) at (182:1) {$D$}; - \node[color=mymagenta] (v4) at (243:1) {$E$}; - \node[color=mymagenta] (v5) at (304:1) {$F$}; + \path (v0) edge (v1); - \path (v0) edge (v2); - \path (v0) edge (v3); - \path (v0) edge (v4); - \path (v0) edge (v5); - \path (v1) edge (v2); - \path (v1) edge (v3); - \path (v1) edge (v4); - \path (v1) edge (v5); - \path (v2) edge (v3); - \path (v2) edge (v4); - \path (v2) edge (v5); - \path (v3) edge (v4); - \path (v3) edge (v5); - \path (v4) edge (v5); - \end{tikzpicture} + \path (v0) edge (v2); + \path (v0) edge (v3); + \path (v0) edge (v4); + \path (v0) edge (v5); + \path (v1) edge (v2); + \path (v1) edge (v3); + \path (v1) edge (v4); + \path (v1) edge (v5); + \path (v2) edge (v3); + \path (v2) edge (v4); + \path (v2) edge (v5); + \path (v3) edge (v4); + \path (v3) edge (v5); + \path (v4) edge (v5); + +\end{tikzpicture} \end{document}
diff --git a/jupyter_tikz/jupyter_tikz.py b/jupyter_tikz/jupyter_tikz.py index f5ce650..ee18f36 100644 --- a/jupyter_tikz/jupyter_tikz.py +++ b/jupyter_tikz/jupyter_tikz.py @@ -10,6 +10,7 @@ from textwrap import dedent, indent from typing import Any, Literal +import jinja2 from IPython import display from IPython.core.magic import Magics, line_cell_magic, magics_class, needs_local_scope from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring @@ -20,34 +21,30 @@ "You cannot use `--print-jinja` and `--print-tex` at the same time." ) _INPUT_TYPE_CONFLIT_ERR = "You cannot use `--implicit-pic`, `--full-document` or/and `-as=` at the same time." -_JINJA_NOT_INTALLED_ERR = ( - "Template cannot be rendered. Please install jinja2: `$ pip install jinja2`" -) -_NS_NOT_PROVIDED_ERR = 'Namespace must be provided when using `use_jinja`, i.e.: `ns=locals()` or `ns={"my_var": value}`' class TexDocument: """This class provides functionality to create and render a LaTeX document given the full LaTeX code. It can also constructs LaTeX code using Jinja2 templates.""" def __init__( - self, code: str, use_jinja: bool = False, ns: dict[str, Any] | None = None + self, code: str, disable_jinja: bool = False, ns: dict[str, Any] | None = None ): """Initializes the `TexDocument` class. Args: code: LaTeX code to render. - use_jinja: A flag to indicate if the code uses Jinja2 template. + disable_jinja: Disable Jinja2 rendering. ns: A namespace dictionary with the variables to render the Jinja2 template. It must be provided when `use_jinja` is `True`. Raises: ValueError: If `use_jinja` is `True` and `ns` is not provided. """ self._code: str = code.strip() - self._use_jinja: bool = use_jinja - if self._use_jinja and not ns: - raise ValueError(_NS_NOT_PROVIDED_ERR) + self._disable_jinja: bool = disable_jinja + if not ns: + ns = {} - if self._use_jinja: + if not self._disable_jinja: self._render_jinja(ns) @property @@ -89,9 +86,9 @@ def __repr__(self) -> str: params = ", ".join( [ - f'{k if k != "_use_jinja" else "use_jinja"}={self._arg_head(v)}' + f'{k if k != "_disable_jinja" else "disable_jinja"}={self._arg_head(v)}' for k, v in params_dict.items() - if k not in ["_code", "full_latex", "ns"] and v + if k not in ["_code", "full_latex", "tikz_code", "ns"] and v ] ) if params: @@ -248,13 +245,17 @@ def run_latex( self._clearup_latex_garbage(keep_temp) def _render_jinja(self, ns) -> None: - try: - import jinja2 - except ImportError: - raise ImportError(_JINJA_NOT_INTALLED_ERR) - fs_loader = jinja2.FileSystemLoader(os.getcwd()) - tmpl_env = jinja2.Environment(loader=fs_loader) + + tmpl_env = jinja2.Environment( + loader=fs_loader, + block_start_string="(**", # Normal is '{%'. + block_end_string="**)", # Normal is '%}'. + variable_start_string="(*", # Normal is '{{'. + variable_end_string="*)", # Normal is '}}'. + comment_start_string="(~", # Normal is '{#'. + comment_end_string="~)", # Normal is '#}'. + ) tmpl = tmpl_env.from_string(self._code) @@ -293,7 +294,7 @@ def __init__( tikz_libraries: str | None = None, pgfplots_libraries: str | None = None, no_tikz: bool = False, - **kargs, + **kwargs, ): """Initializes the `TexFragment` class. @@ -322,7 +323,7 @@ def __init__( tex_packages, tikz_libraries, pgfplots_libraries, no_tikz ) - super().__init__(code, **kargs) + super().__init__(code, **kwargs) def _build_standalone_preamble( self, @@ -385,7 +386,7 @@ def full_latex(self) -> str: _ARGS = { - "input-type": { # New + "input-type": { "short-arg": "as", "dest": "input_type", "type": str, @@ -393,13 +394,13 @@ def full_latex(self) -> str: "desc": "Type of the input. Possible values are: `full-document`, `standalone-document` and `tikzpicture`", "example": "`-as=full-document`", }, - "implicit-pic": { # Deprecated + "implicit-pic": { "short-arg": "i", "dest": "implicit_pic", "type": bool, "desc": "Alias for `-as=tikzpicture`", }, - "full-document": { # Deprecated + "full-document": { "short-arg": "f", "dest": "full_document", "type": bool, @@ -443,11 +444,11 @@ def full_latex(self) -> str: "desc": "Comma-separated list of pgfplots libraries", "example": "`-pl=groupplots,external`", }, - "use-jinja": { # Changed - "short-arg": "j", - "dest": "use_jinja", + "disable-jinja": { # New + "short-arg": "dj", + "dest": "disable_jinja", "type": bool, - "desc": "Render the code using Jinja2", + "desc": "Disable Jinja2 rendering", }, "print-jinja": { "short-arg": "pj", @@ -619,13 +620,6 @@ def _get_input_type(self, input_type: str) -> str | None: @line_cell_magic @magic_arguments() @_apply_args() - @argument( # Deprecated - "--as-jinja", - dest="as_jinja", - action="store_true", - default=False, - help="Deprecated. Use `--use-jinja` instead.", - ) @argument("code", nargs="?", help="the variable in IPython with the Tex/TikZ code") @needs_local_scope def tikz(self, line, cell: str | None = None, local_ns=None) -> Image | SVG | None: @@ -676,8 +670,6 @@ def tikz(self, line, cell: str | None = None, local_ns=None) -> Image | SVG | No file=sys.stderr, ) return - if self.args.as_jinja: - self.args.use_jinja = True if self.args.print_jinja and self.args.print_tex: print( _PRINT_CONFLICT_ERR, @@ -714,7 +706,7 @@ def tikz(self, line, cell: str | None = None, local_ns=None) -> Image | SVG | No if self.input_type == "full-document": self.tex_obj = TexDocument( - self.src, use_jinja=self.args.use_jinja, ns=local_ns + self.src, disable_jinja=self.args.disable_jinja, ns=local_ns ) else: implicit_tikzpicture = self.input_type == "tikzpicture" @@ -727,7 +719,7 @@ def tikz(self, line, cell: str | None = None, local_ns=None) -> Image | SVG | No tikz_libraries=self.args.tikz_libraries, pgfplots_libraries=self.args.pgfplots_libraries, scale=self.args.scale, - use_jinja=self.args.use_jinja or self.args.print_jinja, + disable_jinja=self.args.disable_jinja, ns=local_ns, ) diff --git a/notebooks/GettingStarted.ipynb b/notebooks/GettingStarted.ipynb index ec51d18..910bd42 100644 --- a/notebooks/GettingStarted.ipynb +++ b/notebooks/GettingStarted.ipynb @@ -205,10 +205,10 @@ "::\n", "\n", " %tikz [-as INPUT_TYPE] [-i] [-f] [-p LATEX_PREAMBLE] [-t TEX_PACKAGES]\n", - " [-nt] [-l TIKZ_LIBRARIES] [-lp PGFPLOTS_LIBRARIES] [-j] [-pj]\n", + " [-nt] [-l TIKZ_LIBRARIES] [-lp PGFPLOTS_LIBRARIES] [-dj] [-pj]\n", " [-pt] [-sc SCALE] [-r] [-d DPI] [-g] [-e] [-k] [-tp TEX_PROGRAM]\n", " [-ta TEX_ARGS] [-nc] [-s SAVE_TIKZ] [-st SAVE_TEX] [-sp SAVE_PDF]\n", - " [-S SAVE_IMAGE] [-sv SAVE_VAR] [--as-jinja]\n", + " [-S SAVE_IMAGE] [-sv SAVE_VAR]\n", " [code]\n", "\n", "Renders a TikZ diagram in a Jupyter notebook cell. This function can be used as both a line magic (%tikz) and a cell magic (%%tikz).\n", @@ -262,7 +262,7 @@ " -lp PGFPLOTS_LIBRARIES, --pgfplots-libraries PGFPLOTS_LIBRARIES\n", " Comma-separated list of pgfplots libraries, e.g.,\n", " `-pl=groupplots,external`.\n", - " -j, --use-jinja Render the code using Jinja2.\n", + " -dj, --disable-jinja Disable Jinja2 rendering.\n", " -pj, --print-jinja Print the rendered Jinja2 template.\n", " -pt, --print-tex Print the full LaTeX document.\n", " -sc SCALE, --scale SCALE\n", @@ -295,7 +295,6 @@ " -sv SAVE_VAR, --save-var SAVE_VAR\n", " Save the TikZ or LaTeX code to an IPython variable,\n", " e.g., `-sv my_var`.\n", - " --as-jinja Deprecated. Use `--use-jinja` instead.\n", "\u001b[1;31mFile:\u001b[0m c:\\users\\lucas\\miniconda3\\lib\\site-packages\\jupyter_tikz\\jupyter_tikz.py" ] }, @@ -2330,11 +2329,7 @@ "To help ensure that TikZ pictures stay aligned with your data, you can use [Jinja2 templates](http://jinja.pocoo.org/docs/latest/templates/).\n", "\n", "> **Note:**\n", - "> Before using it, you must install Jinja2:\n", - "> \n", - "> ```shell\n", - "> pip install jinja2\n", - "> ```\n", + "> Since [version 0.5](https://jupyter-tikz.readthedocs.io/stable/about/changelog/), Jinja2 templates are enabled by default, so it's no longer necessary to use the `-j` flag. The `jinja2` package is automatically installed during the installation of `jupyter_tikz`.\n", "\n", "First, we need to populate some data:" ] @@ -2366,7 +2361,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Then, you can interpret the cell source as a Jinja2 template using the `-j` (or `--use-jinja`) parameter:" + "Since [version 0.5](https://jupyter-tikz.readthedocs.io/stable/about/changelog/) we have modified the standard Jinja2 syntax because `{}` braces clash with LaTeX. The table below shows the differences between the standard Jinja2 syntax and the Jupyter TikZ Jinja syntax:\n", + "\n", + "| Standard Jinja2 Syntax | Jupyter TikZ | Example |\n", + "| :--------------------: | :------------------: | :--------------------------: |\n", + "| `{{ expression }}` | `(** expression **)` | `(** for n1 in range(n) **)` |\n", + "| `{% logic/block %}` | `(* logic/block *)` | `\\node at((* angle *):1);` |\n", + "| `{# comment #}` | `(~ comment ~)` | `(~ This wonโ€™t render ~)` |" ] }, { @@ -2465,21 +2466,22 @@ } ], "source": [ - "%%tikz -j -l=arrows,automata -sc=2\n", + "%%tikz -l=arrows,automata -sc=2\n", "\\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick]\n", " \\tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]\n", - " \n", - " {% for name, angle in nodes.items() -%}\n", - " \\node[color=magenta] (v{{ loop.index0 }}) at ({{ angle }}:1) {${{ name }}$};\n", - " {% endfor -%}\n", - " \n", - " {% for n1 in range(n) -%}\n", - " {% for n2 in range(n) -%}\n", - " {%if n1 < n2 -%}\n", - " \\path (v{{n1}}) edge (v{{n2}});\n", - " {% endif -%}\n", - " {% endfor -%}\n", - " {% endfor -%}\n", + "\n", + " % Nodes\n", + " (** for name, angle in nodes.items() **)(~ For expression ~)\n", + " \\node[color=magenta] (v(* loop.index0 *)) at ((* angle *):1) {$(* name *)$}; \n", + " (** endfor **)\n", + " % Paths\n", + " (** for n1 in range(n) **)\n", + " (** for n2 in range(n) **)\n", + " (** if n1 < n2 **)\n", + " \\path (v(* n1 *)) edge (v(* n2 *));\n", + " (** endif **)\n", + " (** endfor **)\n", + " (** endfor **)\n", "\\end{tikzpicture}" ] }, @@ -2508,7 +2510,7 @@ } ], "source": [ - "%%tikz --use-jinja -as=f -r -d=200\n", + "%%tikz -as=f -r -d=200\n", "\\documentclass[tikz]{standalone}\n", "\\usetikzlibrary{arrows,automata}\n", "\\definecolor{mymagenta}{RGB}{226,0,116}\n", @@ -2516,18 +2518,18 @@ "\\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,\n", " semithick]\n", " \\tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]\n", - " \n", - " {% for name, angle in nodes.items() -%}\n", - " \\node[color=mymagenta] (v{{loop.index0}}) at ({{angle}}:1) {${{name}}$};\n", - " {% endfor -%}\n", - " \n", - " {% for n1 in range(n) -%}\n", - " {% for n2 in range(n) -%}\n", - " {%if n1 < n2 -%}\n", - " \\path (v{{n1}}) edge (v{{n2}});\n", - " {% endif -%}\n", - " {% endfor -%}\n", - " {% endfor -%}\n", + "\n", + " (** for name, angle in nodes.items() **)\n", + " \\node[color=mymagenta] (v(* loop.index0 *)) at((* angle *):1) {$(* name *)$};\n", + " (** endfor **)\n", + "\n", + " (** for n1 in range(n) **)\n", + " (** for n2 in range(n) **)\n", + " (** if n1 < n2 **)\n", + " \\path (v(* n1 *)) edge (v(* n2 *));\n", + " (** endif **)\n", + " (** endfor **)\n", + " (** endfor **)\n", "\\end{tikzpicture}\n", "\\end{document}" ] @@ -2536,11 +2538,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Sometimes, you'll make mistakes. Debugging transpiled code is challenging, especially without a mapping.\n", + "Sometimes, you'll make mistakes. Debugging transpiled code is challenging, especially without a mapping. \n", "\n", - "To assist, you can print the Jinja template using `-pj` (or `--print-jinja`):\n", + "To assist, you can print the Jinja rendered output using the `-pj` (or `--print-jinja`) flag.\n", "\n", - "> **Note:** The saved code file is also the interpolated code source." + "> **Note 1:** The `-pj` printed code is also the source of the interpolated code.\n", + "> \n", + "> **Note 2:** Use a minus (`-`) before and/or after a block for whitespace control. For more information, refer to the [Jinja2 documentation](https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control)." ] }, { @@ -2554,29 +2558,31 @@ "text": [ "\\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick]\n", " \\tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]\n", + "\n", + " \\node[color=magenta] (v0) at(0:1) {$A$};\n", + " \\node[color=magenta] (v1) at(60:1) {$B$};\n", + " \\node[color=magenta] (v2) at(121:1) {$C$};\n", + " \\node[color=magenta] (v3) at(182:1) {$D$};\n", + " \\node[color=magenta] (v4) at(243:1) {$E$};\n", + " \\node[color=magenta] (v5) at(304:1) {$F$};\n", " \n", - " \\node[color=magenta] (v0) at (0:1) {$A$};\n", - " \\node[color=magenta] (v1) at (60:1) {$B$};\n", - " \\node[color=magenta] (v2) at (121:1) {$C$};\n", - " \\node[color=magenta] (v3) at (182:1) {$D$};\n", - " \\node[color=magenta] (v4) at (243:1) {$E$};\n", - " \\node[color=magenta] (v5) at (304:1) {$F$};\n", " \\path (v0) edge (v1);\n", - " \\path (v0) edge (v2);\n", - " \\path (v0) edge (v3);\n", - " \\path (v0) edge (v4);\n", - " \\path (v0) edge (v5);\n", - " \\path (v1) edge (v2);\n", - " \\path (v1) edge (v3);\n", - " \\path (v1) edge (v4);\n", - " \\path (v1) edge (v5);\n", - " \\path (v2) edge (v3);\n", - " \\path (v2) edge (v4);\n", - " \\path (v2) edge (v5);\n", - " \\path (v3) edge (v4);\n", - " \\path (v3) edge (v5);\n", - " \\path (v4) edge (v5);\n", - " \\end{tikzpicture}\n" + " \\path (v0) edge (v2);\n", + " \\path (v0) edge (v3);\n", + " \\path (v0) edge (v4);\n", + " \\path (v0) edge (v5);\n", + " \\path (v1) edge (v2);\n", + " \\path (v1) edge (v3);\n", + " \\path (v1) edge (v4);\n", + " \\path (v1) edge (v5);\n", + " \\path (v2) edge (v3);\n", + " \\path (v2) edge (v4);\n", + " \\path (v2) edge (v5);\n", + " \\path (v3) edge (v4);\n", + " \\path (v3) edge (v5);\n", + " \\path (v4) edge (v5);\n", + " \n", + "\\end{tikzpicture}\n" ] }, { @@ -2673,19 +2679,188 @@ "%%tikz -pj -l=arrows,automata -sc=2 --save-tex=outputs/jinja_rendered.tex\n", "\\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick]\n", " \\tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]\n", - " \n", - " {% for name, angle in nodes.items() -%}\n", - " \\node[color=magenta] (v{{ loop.index0 }}) at ({{ angle }}:1) {${{ name }}$};\n", - " {% endfor -%}\n", - " \n", - " {% for n1 in range(n) -%}\n", - " {% for n2 in range(n) -%}\n", - " {%if n1 < n2 -%}\n", - " \\path (v{{n1}}) edge (v{{n2}});\n", - " {% endif -%}\n", - " {% endfor -%}\n", - " {% endfor -%}\n", "\n", + " (~ Using minus `-` before/after each block to whitespace control. -~)\n", + " (~- https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control -~)\n", + " (** for name, angle in nodes.items() -**)\n", + " \\node[color=magenta] (v(* loop.index0 *)) at((* angle *):1) {$(* name *)$};\n", + " (** endfor **)\n", + " (** for n1 in range(n) -**)\n", + " (** for n2 in range(n) -**)\n", + " (**- if n1 < n2 -**)\n", + " \\path (v(* n1 *)) edge (v(* n2 *));\n", + " (** endif -**)\n", + " (** endfor -**)\n", + "(** endfor **)\n", + "\\end{tikzpicture}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you don't want to use Jinja2 rendering you can tell it using the flag `-dj` (or `--disable-jinja`):" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%%tikz -dj -sc=2\n", + "\\begin{tikzpicture}\n", + " \\node {(* Show `(*` because i'm not rendering Jinja*)};\n", "\\end{tikzpicture}" ] }, @@ -2715,7 +2890,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -2736,7 +2911,7 @@ "" ] }, - "execution_count": 36, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -2755,7 +2930,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -2777,15 +2952,15 @@ "" ] }, - "execution_count": 37, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "%%tikz -as=t --use-jinja\n", + "%%tikz -as=t \n", "\\begin{tikzpicture}[scale=3]\n", - " {{ my_frame }} % This is my_frame that I rendered before\n", + " (* my_frame -*) % This is my_frame that I rendered before\n", " \\filldraw (0.5,0.5) circle (.1);\n", "\\end{tikzpicture}" ] @@ -2794,12 +2969,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can also combine `-sv` with `-j`:" + "You can also combine `-sv` with Jinja blocks:" ] }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -2852,30 +3027,30 @@ "" ] }, - "execution_count": 38, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "%%tikz -as=t -j -sv=node_names -sc=2\n", - "{% for name, angle in nodes.items() -%}\n", - " \\node[color=red] (v{{loop.index0}}) at ({{angle}}:1) {${{name}}$};\n", - "{% endfor -%}" + "%%tikz -as=t -sv=node_names -sc=2\n", + "(** for name, angle in nodes.items() -**)\n", + " \\node[color=red] (v(* loop.index0 *)) at((* angle *):1) {$(* name *)$};\n", + "(** endfor -**)" ] }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'\\\\node[color=red] (v0) at (0:1) {$A$};\\n\\\\node[color=red] (v1) at (60:1) {$B$};\\n\\\\node[color=red] (v2) at (121:1) {$C$};\\n\\\\node[color=red] (v3) at (182:1) {$D$};\\n\\\\node[color=red] (v4) at (243:1) {$E$};\\n\\\\node[color=red] (v5) at (304:1) {$F$};\\n'" + "'\\\\node[color=red] (v0) at(0:1) {$A$};\\n\\\\node[color=red] (v1) at(60:1) {$B$};\\n\\\\node[color=red] (v2) at(121:1) {$C$};\\n\\\\node[color=red] (v3) at(182:1) {$D$};\\n\\\\node[color=red] (v4) at(243:1) {$E$};\\n\\\\node[color=red] (v5) at(304:1) {$F$};\\n'" ] }, - "execution_count": 39, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -2893,7 +3068,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -2981,26 +3156,25 @@ "" ] }, - "execution_count": 40, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "%%tikz --use-jinja -l=arrows,automata -sc=2\n", + "%%tikz -l=arrows,automata -sc=2\n", "\\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick]\n", " \\tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]\n", - " \n", - " {{ node_names }}\n", - " \n", - " {% for n1 in range(n) -%}\n", - " {% for n2 in range(n) -%}\n", - " {%if n1 < n2 -%}\n", - " \\path (v{{n1}}) edge (v{{n2}});\n", - " {% endif -%}\n", - " {% endfor -%}\n", - " {% endfor -%}\n", "\n", + " (* node_names *)\n", + " \n", + " (** for n1 in range(n) -**)\n", + " (** for n2 in range(n) -**)\n", + " (** if n1 < n2 -**)\n", + " \\path (v(* n1 *)) edge (v(* n2 *));\n", + " (** endif **)\n", + " (** endfor **)\n", + " (** endfor **)\n", "\\end{tikzpicture}" ] }, @@ -3013,7 +3187,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ @@ -3032,7 +3206,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -3054,7 +3228,7 @@ "" ] }, - "execution_count": 42, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -3083,7 +3257,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -3258,7 +3432,7 @@ "" ] }, - "execution_count": 43, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } @@ -3282,7 +3456,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -3291,7 +3465,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 46, "metadata": {}, "outputs": [ { @@ -3466,7 +3640,7 @@ "" ] }, - "execution_count": 45, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } @@ -3499,7 +3673,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 47, "metadata": {}, "outputs": [ { @@ -3544,7 +3718,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 48, "metadata": { "scrolled": true }, @@ -3763,7 +3937,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 49, "metadata": {}, "outputs": [ { @@ -3810,7 +3984,7 @@ "" ] }, - "execution_count": 48, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } @@ -3834,7 +4008,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 50, "metadata": {}, "outputs": [ { @@ -3860,7 +4034,7 @@ "" ] }, - "execution_count": 49, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -3877,7 +4051,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ @@ -3907,7 +4081,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ @@ -3939,7 +4113,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 53, "metadata": {}, "outputs": [], "source": [ @@ -3975,7 +4149,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 54, "metadata": {}, "outputs": [ { @@ -4022,7 +4196,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 55, "metadata": {}, "outputs": [ { @@ -4144,7 +4318,7 @@ "" ] }, - "execution_count": 54, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -4162,7 +4336,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -4284,7 +4458,7 @@ "" ] }, - "execution_count": 55, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -4300,9 +4474,16 @@ "## Working with Jinja" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can render Jinja by simply creating an object with Jinja code:" + ] + }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -4313,18 +4494,18 @@ "\\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,\n", " semithick]\n", " \\tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]\n", - " \n", - " {% for name, angle in nodes.items() -%}\n", - " \\node[color=mymagenta] (v{{loop.index0}}) at ({{angle}}:1) {${{name}}$};\n", - " {% endfor -%}\n", - " \n", - " {% for n1 in range(n) -%}\n", - " {% for n2 in range(n) -%}\n", - " {%if n1 < n2 -%}\n", - " \\path (v{{n1}}) edge (v{{n2}});\n", - " {% endif -%}\n", - " {% endfor -%}\n", - " {% endfor -%}\n", + "\n", + " (** for name, angle in nodes.items() -**)\n", + " \\node[color=mymagenta] (v(* loop.index0 *)) at((* angle *):1) {$(* name *)$};\n", + " (** endfor **)\n", + "\n", + " (** for n1 in range(n) -**)\n", + " (** for n2 in range(n) -**)\n", + " (** if n1 < n2 -**)\n", + " \\path (v(* n1 *)) edge (v(* n2 *));\n", + " (** endif -**)\n", + " (** endfor -**)\n", + " (** endfor **)\n", "\\end{tikzpicture}\n", "\\end{document}\"\"\"" ] @@ -4333,14 +4514,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can render Jinja by passing the `use-jinja` option:\n", - "\n", - "> **Note:** If you are using Jinja, you must pass the `ns=` parameter in order to allow the method to access the variables:" + "> **Note:** You must pass the `ns=` parameter in other to allow the method to access the variable." ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -4354,35 +4533,38 @@ "\\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,\n", " semithick]\n", " \\tikzstyle{every state}=[fill=mymagenta,draw=none,text=white]\n", + "\n", + " \\node[color=mymagenta] (v0) at(0:1) {$A$};\n", + " \\node[color=mymagenta] (v1) at(60:1) {$B$};\n", + " \\node[color=mymagenta] (v2) at(121:1) {$C$};\n", + " \\node[color=mymagenta] (v3) at(182:1) {$D$};\n", + " \\node[color=mymagenta] (v4) at(243:1) {$E$};\n", + " \\node[color=mymagenta] (v5) at(304:1) {$F$};\n", " \n", - " \\node[color=mymagenta] (v0) at (0:1) {$A$};\n", - " \\node[color=mymagenta] (v1) at (60:1) {$B$};\n", - " \\node[color=mymagenta] (v2) at (121:1) {$C$};\n", - " \\node[color=mymagenta] (v3) at (182:1) {$D$};\n", - " \\node[color=mymagenta] (v4) at (243:1) {$E$};\n", - " \\node[color=mymagenta] (v5) at (304:1) {$F$};\n", + "\n", " \\path (v0) edge (v1);\n", - " \\path (v0) edge (v2);\n", - " \\path (v0) edge (v3);\n", - " \\path (v0) edge (v4);\n", - " \\path (v0) edge (v5);\n", - " \\path (v1) edge (v2);\n", - " \\path (v1) edge (v3);\n", - " \\path (v1) edge (v4);\n", - " \\path (v1) edge (v5);\n", - " \\path (v2) edge (v3);\n", - " \\path (v2) edge (v4);\n", - " \\path (v2) edge (v5);\n", - " \\path (v3) edge (v4);\n", - " \\path (v3) edge (v5);\n", - " \\path (v4) edge (v5);\n", - " \\end{tikzpicture}\n", + " \\path (v0) edge (v2);\n", + " \\path (v0) edge (v3);\n", + " \\path (v0) edge (v4);\n", + " \\path (v0) edge (v5);\n", + " \\path (v1) edge (v2);\n", + " \\path (v1) edge (v3);\n", + " \\path (v1) edge (v4);\n", + " \\path (v1) edge (v5);\n", + " \\path (v2) edge (v3);\n", + " \\path (v2) edge (v4);\n", + " \\path (v2) edge (v5);\n", + " \\path (v3) edge (v4);\n", + " \\path (v3) edge (v5);\n", + " \\path (v4) edge (v5);\n", + " \n", + "\\end{tikzpicture}\n", "\\end{document}\n" ] } ], "source": [ - "tex_jinja_document = TexDocument(jinja_code, use_jinja=True, ns=locals())\n", + "tex_jinja_document = TexDocument(jinja_code, ns=locals())\n", "print(tex_jinja_document) # It prints the rendered Jinja Code" ] }, @@ -4395,7 +4577,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 59, "metadata": {}, "outputs": [ { @@ -4405,7 +4587,7 @@ "" ] }, - "execution_count": 58, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -4434,7 +4616,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 60, "metadata": {}, "outputs": [], "source": [ @@ -4449,7 +4631,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 61, "metadata": {}, "outputs": [], "source": [ @@ -4465,7 +4647,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 62, "metadata": { "scrolled": true }, @@ -4642,7 +4824,7 @@ "" ] }, - "execution_count": 61, + "execution_count": 62, "metadata": {}, "output_type": "execute_result" } @@ -4661,7 +4843,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 63, "metadata": {}, "outputs": [ { @@ -4671,7 +4853,7 @@ "" ] }, - "execution_count": 62, + "execution_count": 63, "metadata": {}, "output_type": "execute_result" } @@ -4684,7 +4866,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 64, "metadata": {}, "outputs": [], "source": [ @@ -4700,7 +4882,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 65, "metadata": {}, "outputs": [ { @@ -4710,7 +4892,7 @@ "" ] }, - "execution_count": 64, + "execution_count": 65, "metadata": {}, "output_type": "execute_result" } @@ -4727,7 +4909,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 66, "metadata": {}, "outputs": [ { @@ -4753,7 +4935,7 @@ "" ] }, - "execution_count": 65, + "execution_count": 66, "metadata": {}, "output_type": "execute_result" } @@ -4772,7 +4954,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 67, "metadata": {}, "outputs": [ { @@ -4887,7 +5069,7 @@ "" ] }, - "execution_count": 66, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } diff --git a/notebooks/outputs/a_dot.pdf b/notebooks/outputs/a_dot.pdf index e11049c..067758a 100644 Binary files a/notebooks/outputs/a_dot.pdf and b/notebooks/outputs/a_dot.pdf differ diff --git a/notebooks/outputs/angle.pdf b/notebooks/outputs/angle.pdf index 5b8d60d..f710dc8 100644 Binary files a/notebooks/outputs/angle.pdf and b/notebooks/outputs/angle.pdf differ diff --git a/notebooks/outputs/jinja_rendered.tex b/notebooks/outputs/jinja_rendered.tex index 10eafc8..ba2ba9d 100644 --- a/notebooks/outputs/jinja_rendered.tex +++ b/notebooks/outputs/jinja_rendered.tex @@ -6,28 +6,30 @@ \scalebox{2.0}{ \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm, semithick] \tikzstyle{every state}=[fill=mymagenta,draw=none,text=white] + + \node[color=magenta] (v0) at(0:1) {$A$}; + \node[color=magenta] (v1) at(60:1) {$B$}; + \node[color=magenta] (v2) at(121:1) {$C$}; + \node[color=magenta] (v3) at(182:1) {$D$}; + \node[color=magenta] (v4) at(243:1) {$E$}; + \node[color=magenta] (v5) at(304:1) {$F$}; - \node[color=magenta] (v0) at (0:1) {$A$}; - \node[color=magenta] (v1) at (60:1) {$B$}; - \node[color=magenta] (v2) at (121:1) {$C$}; - \node[color=magenta] (v3) at (182:1) {$D$}; - \node[color=magenta] (v4) at (243:1) {$E$}; - \node[color=magenta] (v5) at (304:1) {$F$}; \path (v0) edge (v1); - \path (v0) edge (v2); - \path (v0) edge (v3); - \path (v0) edge (v4); - \path (v0) edge (v5); - \path (v1) edge (v2); - \path (v1) edge (v3); - \path (v1) edge (v4); - \path (v1) edge (v5); - \path (v2) edge (v3); - \path (v2) edge (v4); - \path (v2) edge (v5); - \path (v3) edge (v4); - \path (v3) edge (v5); - \path (v4) edge (v5); - \end{tikzpicture} + \path (v0) edge (v2); + \path (v0) edge (v3); + \path (v0) edge (v4); + \path (v0) edge (v5); + \path (v1) edge (v2); + \path (v1) edge (v3); + \path (v1) edge (v4); + \path (v1) edge (v5); + \path (v2) edge (v3); + \path (v2) edge (v4); + \path (v2) edge (v5); + \path (v3) edge (v4); + \path (v3) edge (v5); + \path (v4) edge (v5); + + \end{tikzpicture} } \end{document} \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 89662f5..c20cc6d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -32,6 +32,17 @@ files = [ [package.extras] dev = ["freezegun (>=1.0,<2.0)", "pytest (>=6.0)", "pytest-cov"] +[[package]] +name = "backports-strenum" +version = "1.3.1" +description = "Base class for creating enumerated constants that are also subclasses of str" +optional = false +python-versions = ">=3.8.6,<3.11" +files = [ + {file = "backports_strenum-1.3.1-py3-none-any.whl", hash = "sha256:cdcfe36dc897e2615dc793b7d3097f54d359918fc448754a517e6f23044ccf83"}, + {file = "backports_strenum-1.3.1.tar.gz", hash = "sha256:77c52407342898497714f0596e86188bb7084f89063226f4ba66863482f42414"}, +] + [[package]] name = "black" version = "24.4.2" @@ -320,63 +331,63 @@ files = [ [[package]] name = "coverage" -version = "7.5.4" +version = "7.6.0" description = "Code coverage measurement for Python" optional = false python-versions = ">=3.8" files = [ - {file = "coverage-7.5.4-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:6cfb5a4f556bb51aba274588200a46e4dd6b505fb1a5f8c5ae408222eb416f99"}, - {file = "coverage-7.5.4-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:2174e7c23e0a454ffe12267a10732c273243b4f2d50d07544a91198f05c48f47"}, - {file = "coverage-7.5.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2214ee920787d85db1b6a0bd9da5f8503ccc8fcd5814d90796c2f2493a2f4d2e"}, - {file = "coverage-7.5.4-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1137f46adb28e3813dec8c01fefadcb8c614f33576f672962e323b5128d9a68d"}, - {file = "coverage-7.5.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b385d49609f8e9efc885790a5a0e89f2e3ae042cdf12958b6034cc442de428d3"}, - {file = "coverage-7.5.4-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:b4a474f799456e0eb46d78ab07303286a84a3140e9700b9e154cfebc8f527016"}, - {file = "coverage-7.5.4-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:5cd64adedf3be66f8ccee418473c2916492d53cbafbfcff851cbec5a8454b136"}, - {file = "coverage-7.5.4-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:e564c2cf45d2f44a9da56f4e3a26b2236504a496eb4cb0ca7221cd4cc7a9aca9"}, - {file = "coverage-7.5.4-cp310-cp310-win32.whl", hash = "sha256:7076b4b3a5f6d2b5d7f1185fde25b1e54eb66e647a1dfef0e2c2bfaf9b4c88c8"}, - {file = "coverage-7.5.4-cp310-cp310-win_amd64.whl", hash = "sha256:018a12985185038a5b2bcafab04ab833a9a0f2c59995b3cec07e10074c78635f"}, - {file = "coverage-7.5.4-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:db14f552ac38f10758ad14dd7b983dbab424e731588d300c7db25b6f89e335b5"}, - {file = "coverage-7.5.4-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3257fdd8e574805f27bb5342b77bc65578e98cbc004a92232106344053f319ba"}, - {file = "coverage-7.5.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3a6612c99081d8d6134005b1354191e103ec9705d7ba2754e848211ac8cacc6b"}, - {file = "coverage-7.5.4-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d45d3cbd94159c468b9b8c5a556e3f6b81a8d1af2a92b77320e887c3e7a5d080"}, - {file = "coverage-7.5.4-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ed550e7442f278af76d9d65af48069f1fb84c9f745ae249c1a183c1e9d1b025c"}, - {file = "coverage-7.5.4-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:7a892be37ca35eb5019ec85402c3371b0f7cda5ab5056023a7f13da0961e60da"}, - {file = "coverage-7.5.4-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:8192794d120167e2a64721d88dbd688584675e86e15d0569599257566dec9bf0"}, - {file = "coverage-7.5.4-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:820bc841faa502e727a48311948e0461132a9c8baa42f6b2b84a29ced24cc078"}, - {file = "coverage-7.5.4-cp311-cp311-win32.whl", hash = "sha256:6aae5cce399a0f065da65c7bb1e8abd5c7a3043da9dceb429ebe1b289bc07806"}, - {file = "coverage-7.5.4-cp311-cp311-win_amd64.whl", hash = "sha256:d2e344d6adc8ef81c5a233d3a57b3c7d5181f40e79e05e1c143da143ccb6377d"}, - {file = "coverage-7.5.4-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:54317c2b806354cbb2dc7ac27e2b93f97096912cc16b18289c5d4e44fc663233"}, - {file = "coverage-7.5.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:042183de01f8b6d531e10c197f7f0315a61e8d805ab29c5f7b51a01d62782747"}, - {file = "coverage-7.5.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a6bb74ed465d5fb204b2ec41d79bcd28afccf817de721e8a807d5141c3426638"}, - {file = "coverage-7.5.4-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:b3d45ff86efb129c599a3b287ae2e44c1e281ae0f9a9bad0edc202179bcc3a2e"}, - {file = "coverage-7.5.4-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5013ed890dc917cef2c9f765c4c6a8ae9df983cd60dbb635df8ed9f4ebc9f555"}, - {file = "coverage-7.5.4-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:1014fbf665fef86cdfd6cb5b7371496ce35e4d2a00cda501cf9f5b9e6fced69f"}, - {file = "coverage-7.5.4-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:3684bc2ff328f935981847082ba4fdc950d58906a40eafa93510d1b54c08a66c"}, - {file = "coverage-7.5.4-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:581ea96f92bf71a5ec0974001f900db495488434a6928a2ca7f01eee20c23805"}, - {file = "coverage-7.5.4-cp312-cp312-win32.whl", hash = "sha256:73ca8fbc5bc622e54627314c1a6f1dfdd8db69788f3443e752c215f29fa87a0b"}, - {file = "coverage-7.5.4-cp312-cp312-win_amd64.whl", hash = "sha256:cef4649ec906ea7ea5e9e796e68b987f83fa9a718514fe147f538cfeda76d7a7"}, - {file = "coverage-7.5.4-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:cdd31315fc20868c194130de9ee6bfd99755cc9565edff98ecc12585b90be882"}, - {file = "coverage-7.5.4-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:02ff6e898197cc1e9fa375581382b72498eb2e6d5fc0b53f03e496cfee3fac6d"}, - {file = "coverage-7.5.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d05c16cf4b4c2fc880cb12ba4c9b526e9e5d5bb1d81313d4d732a5b9fe2b9d53"}, - {file = "coverage-7.5.4-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c5986ee7ea0795a4095ac4d113cbb3448601efca7f158ec7f7087a6c705304e4"}, - {file = "coverage-7.5.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5df54843b88901fdc2f598ac06737f03d71168fd1175728054c8f5a2739ac3e4"}, - {file = "coverage-7.5.4-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:ab73b35e8d109bffbda9a3e91c64e29fe26e03e49addf5b43d85fc426dde11f9"}, - {file = "coverage-7.5.4-cp38-cp38-musllinux_1_2_i686.whl", hash = "sha256:aea072a941b033813f5e4814541fc265a5c12ed9720daef11ca516aeacd3bd7f"}, - {file = "coverage-7.5.4-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:16852febd96acd953b0d55fc842ce2dac1710f26729b31c80b940b9afcd9896f"}, - {file = "coverage-7.5.4-cp38-cp38-win32.whl", hash = "sha256:8f894208794b164e6bd4bba61fc98bf6b06be4d390cf2daacfa6eca0a6d2bb4f"}, - {file = "coverage-7.5.4-cp38-cp38-win_amd64.whl", hash = "sha256:e2afe743289273209c992075a5a4913e8d007d569a406ffed0bd080ea02b0633"}, - {file = "coverage-7.5.4-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:b95c3a8cb0463ba9f77383d0fa8c9194cf91f64445a63fc26fb2327e1e1eb088"}, - {file = "coverage-7.5.4-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:3d7564cc09dd91b5a6001754a5b3c6ecc4aba6323baf33a12bd751036c998be4"}, - {file = "coverage-7.5.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:44da56a2589b684813f86d07597fdf8a9c6ce77f58976727329272f5a01f99f7"}, - {file = "coverage-7.5.4-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e16f3d6b491c48c5ae726308e6ab1e18ee830b4cdd6913f2d7f77354b33f91c8"}, - {file = "coverage-7.5.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dbc5958cb471e5a5af41b0ddaea96a37e74ed289535e8deca404811f6cb0bc3d"}, - {file = "coverage-7.5.4-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:a04e990a2a41740b02d6182b498ee9796cf60eefe40cf859b016650147908029"}, - {file = "coverage-7.5.4-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:ddbd2f9713a79e8e7242d7c51f1929611e991d855f414ca9996c20e44a895f7c"}, - {file = "coverage-7.5.4-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:b1ccf5e728ccf83acd313c89f07c22d70d6c375a9c6f339233dcf792094bcbf7"}, - {file = "coverage-7.5.4-cp39-cp39-win32.whl", hash = "sha256:56b4eafa21c6c175b3ede004ca12c653a88b6f922494b023aeb1e836df953ace"}, - {file = "coverage-7.5.4-cp39-cp39-win_amd64.whl", hash = "sha256:65e528e2e921ba8fd67d9055e6b9f9e34b21ebd6768ae1c1723f4ea6ace1234d"}, - {file = "coverage-7.5.4-pp38.pp39.pp310-none-any.whl", hash = "sha256:79b356f3dd5b26f3ad23b35c75dbdaf1f9e2450b6bcefc6d0825ea0aa3f86ca5"}, - {file = "coverage-7.5.4.tar.gz", hash = "sha256:a44963520b069e12789d0faea4e9fdb1e410cdc4aab89d94f7f55cbb7fef0353"}, + {file = "coverage-7.6.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:dff044f661f59dace805eedb4a7404c573b6ff0cdba4a524141bc63d7be5c7fd"}, + {file = "coverage-7.6.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:a8659fd33ee9e6ca03950cfdcdf271d645cf681609153f218826dd9805ab585c"}, + {file = "coverage-7.6.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7792f0ab20df8071d669d929c75c97fecfa6bcab82c10ee4adb91c7a54055463"}, + {file = "coverage-7.6.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d4b3cd1ca7cd73d229487fa5caca9e4bc1f0bca96526b922d61053ea751fe791"}, + {file = "coverage-7.6.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e7e128f85c0b419907d1f38e616c4f1e9f1d1b37a7949f44df9a73d5da5cd53c"}, + {file = "coverage-7.6.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:a94925102c89247530ae1dab7dc02c690942566f22e189cbd53579b0693c0783"}, + {file = "coverage-7.6.0-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:dcd070b5b585b50e6617e8972f3fbbee786afca71b1936ac06257f7e178f00f6"}, + {file = "coverage-7.6.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:d50a252b23b9b4dfeefc1f663c568a221092cbaded20a05a11665d0dbec9b8fb"}, + {file = "coverage-7.6.0-cp310-cp310-win32.whl", hash = "sha256:0e7b27d04131c46e6894f23a4ae186a6a2207209a05df5b6ad4caee6d54a222c"}, + {file = "coverage-7.6.0-cp310-cp310-win_amd64.whl", hash = "sha256:54dece71673b3187c86226c3ca793c5f891f9fc3d8aa183f2e3653da18566169"}, + {file = "coverage-7.6.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:c7b525ab52ce18c57ae232ba6f7010297a87ced82a2383b1afd238849c1ff933"}, + {file = "coverage-7.6.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:4bea27c4269234e06f621f3fac3925f56ff34bc14521484b8f66a580aacc2e7d"}, + {file = "coverage-7.6.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ed8d1d1821ba5fc88d4a4f45387b65de52382fa3ef1f0115a4f7a20cdfab0e94"}, + {file = "coverage-7.6.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:01c322ef2bbe15057bc4bf132b525b7e3f7206f071799eb8aa6ad1940bcf5fb1"}, + {file = "coverage-7.6.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:03cafe82c1b32b770a29fd6de923625ccac3185a54a5e66606da26d105f37dac"}, + {file = "coverage-7.6.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:0d1b923fc4a40c5832be4f35a5dab0e5ff89cddf83bb4174499e02ea089daf57"}, + {file = "coverage-7.6.0-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:4b03741e70fb811d1a9a1d75355cf391f274ed85847f4b78e35459899f57af4d"}, + {file = "coverage-7.6.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:a73d18625f6a8a1cbb11eadc1d03929f9510f4131879288e3f7922097a429f63"}, + {file = "coverage-7.6.0-cp311-cp311-win32.whl", hash = "sha256:65fa405b837060db569a61ec368b74688f429b32fa47a8929a7a2f9b47183713"}, + {file = "coverage-7.6.0-cp311-cp311-win_amd64.whl", hash = "sha256:6379688fb4cfa921ae349c76eb1a9ab26b65f32b03d46bb0eed841fd4cb6afb1"}, + {file = "coverage-7.6.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:f7db0b6ae1f96ae41afe626095149ecd1b212b424626175a6633c2999eaad45b"}, + {file = "coverage-7.6.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:bbdf9a72403110a3bdae77948b8011f644571311c2fb35ee15f0f10a8fc082e8"}, + {file = "coverage-7.6.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9cc44bf0315268e253bf563f3560e6c004efe38f76db03a1558274a6e04bf5d5"}, + {file = "coverage-7.6.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:da8549d17489cd52f85a9829d0e1d91059359b3c54a26f28bec2c5d369524807"}, + {file = "coverage-7.6.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0086cd4fc71b7d485ac93ca4239c8f75732c2ae3ba83f6be1c9be59d9e2c6382"}, + {file = "coverage-7.6.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:1fad32ee9b27350687035cb5fdf9145bc9cf0a094a9577d43e909948ebcfa27b"}, + {file = "coverage-7.6.0-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:044a0985a4f25b335882b0966625270a8d9db3d3409ddc49a4eb00b0ef5e8cee"}, + {file = "coverage-7.6.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:76d5f82213aa78098b9b964ea89de4617e70e0d43e97900c2778a50856dac605"}, + {file = "coverage-7.6.0-cp312-cp312-win32.whl", hash = "sha256:3c59105f8d58ce500f348c5b56163a4113a440dad6daa2294b5052a10db866da"}, + {file = "coverage-7.6.0-cp312-cp312-win_amd64.whl", hash = "sha256:ca5d79cfdae420a1d52bf177de4bc2289c321d6c961ae321503b2ca59c17ae67"}, + {file = "coverage-7.6.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:d39bd10f0ae453554798b125d2f39884290c480f56e8a02ba7a6ed552005243b"}, + {file = "coverage-7.6.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:beb08e8508e53a568811016e59f3234d29c2583f6b6e28572f0954a6b4f7e03d"}, + {file = "coverage-7.6.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b2e16f4cd2bc4d88ba30ca2d3bbf2f21f00f382cf4e1ce3b1ddc96c634bc48ca"}, + {file = "coverage-7.6.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6616d1c9bf1e3faea78711ee42a8b972367d82ceae233ec0ac61cc7fec09fa6b"}, + {file = "coverage-7.6.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ad4567d6c334c46046d1c4c20024de2a1c3abc626817ae21ae3da600f5779b44"}, + {file = "coverage-7.6.0-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:d17c6a415d68cfe1091d3296ba5749d3d8696e42c37fca5d4860c5bf7b729f03"}, + {file = "coverage-7.6.0-cp38-cp38-musllinux_1_2_i686.whl", hash = "sha256:9146579352d7b5f6412735d0f203bbd8d00113a680b66565e205bc605ef81bc6"}, + {file = "coverage-7.6.0-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:cdab02a0a941af190df8782aafc591ef3ad08824f97850b015c8c6a8b3877b0b"}, + {file = "coverage-7.6.0-cp38-cp38-win32.whl", hash = "sha256:df423f351b162a702c053d5dddc0fc0ef9a9e27ea3f449781ace5f906b664428"}, + {file = "coverage-7.6.0-cp38-cp38-win_amd64.whl", hash = "sha256:f2501d60d7497fd55e391f423f965bbe9e650e9ffc3c627d5f0ac516026000b8"}, + {file = "coverage-7.6.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:7221f9ac9dad9492cecab6f676b3eaf9185141539d5c9689d13fd6b0d7de840c"}, + {file = "coverage-7.6.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:ddaaa91bfc4477d2871442bbf30a125e8fe6b05da8a0015507bfbf4718228ab2"}, + {file = "coverage-7.6.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c4cbe651f3904e28f3a55d6f371203049034b4ddbce65a54527a3f189ca3b390"}, + {file = "coverage-7.6.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:831b476d79408ab6ccfadaaf199906c833f02fdb32c9ab907b1d4aa0713cfa3b"}, + {file = "coverage-7.6.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:46c3d091059ad0b9c59d1034de74a7f36dcfa7f6d3bde782c49deb42438f2450"}, + {file = "coverage-7.6.0-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:4d5fae0a22dc86259dee66f2cc6c1d3e490c4a1214d7daa2a93d07491c5c04b6"}, + {file = "coverage-7.6.0-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:07ed352205574aad067482e53dd606926afebcb5590653121063fbf4e2175166"}, + {file = "coverage-7.6.0-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:49c76cdfa13015c4560702574bad67f0e15ca5a2872c6a125f6327ead2b731dd"}, + {file = "coverage-7.6.0-cp39-cp39-win32.whl", hash = "sha256:482855914928c8175735a2a59c8dc5806cf7d8f032e4820d52e845d1f731dca2"}, + {file = "coverage-7.6.0-cp39-cp39-win_amd64.whl", hash = "sha256:543ef9179bc55edfd895154a51792b01c017c87af0ebaae092720152e19e42ca"}, + {file = "coverage-7.6.0-pp38.pp39.pp310-none-any.whl", hash = "sha256:6fe885135c8a479d3e37a7aae61cbd3a0fb2deccb4dda3c25f92a49189f766d6"}, + {file = "coverage-7.6.0.tar.gz", hash = "sha256:289cc803fa1dc901f84701ac10c9ee873619320f2f9aff38794db4a4a0268d51"}, ] [package.dependencies] @@ -428,13 +439,13 @@ files = [ [[package]] name = "exceptiongroup" -version = "1.2.1" +version = "1.2.2" description = "Backport of PEP 654 (exception groups)" optional = false python-versions = ">=3.7" files = [ - {file = "exceptiongroup-1.2.1-py3-none-any.whl", hash = "sha256:5258b9ed329c5bbdd31a309f53cbfb0b155341807f6ff7606a1e801a891b29ad"}, - {file = "exceptiongroup-1.2.1.tar.gz", hash = "sha256:a4785e48b045528f5bfe627b6ad554ff32def154f42372786903b7abcfe1aa16"}, + {file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"}, + {file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"}, ] [package.extras] @@ -473,16 +484,17 @@ dev = ["flake8", "markdown", "twine", "wheel"] [[package]] name = "griffe" -version = "0.47.0" +version = "0.48.0" description = "Signatures for entire Python programs. Extract the structure, the frame, the skeleton of your project, to generate API documentation or find breaking changes in your API." optional = false python-versions = ">=3.8" files = [ - {file = "griffe-0.47.0-py3-none-any.whl", hash = "sha256:07a2fd6a8c3d21d0bbb0decf701d62042ccc8a576645c7f8799fe1f10de2b2de"}, - {file = "griffe-0.47.0.tar.gz", hash = "sha256:95119a440a3c932b13293538bdbc405bee4c36428547553dc6b327e7e7d35e5a"}, + {file = "griffe-0.48.0-py3-none-any.whl", hash = "sha256:f944c6ff7bd31cf76f264adcd6ab8f3d00a2f972ae5cc8db2d7b6dcffeff65a2"}, + {file = "griffe-0.48.0.tar.gz", hash = "sha256:f099461c02f016b6be4af386d5aa92b01fb4efe6c1c2c360dda9a5d0a863bb7f"}, ] [package.dependencies] +backports-strenum = {version = ">=1.3", markers = "python_version < \"3.11\""} colorama = ">=0.4" [[package]] @@ -831,13 +843,13 @@ files = [ [[package]] name = "mkdocstrings" -version = "0.25.1" +version = "0.25.2" description = "Automatic documentation from sources, for MkDocs." optional = false python-versions = ">=3.8" files = [ - {file = "mkdocstrings-0.25.1-py3-none-any.whl", hash = "sha256:da01fcc2670ad61888e8fe5b60afe9fee5781017d67431996832d63e887c2e51"}, - {file = "mkdocstrings-0.25.1.tar.gz", hash = "sha256:c3a2515f31577f311a9ee58d089e4c51fc6046dbd9e9b4c3de4c3194667fe9bf"}, + {file = "mkdocstrings-0.25.2-py3-none-any.whl", hash = "sha256:9e2cda5e2e12db8bb98d21e3410f3f27f8faab685a24b03b06ba7daa5b92abfc"}, + {file = "mkdocstrings-0.25.2.tar.gz", hash = "sha256:5cf57ad7f61e8be3111a2458b4e49c2029c9cb35525393b179f9c916ca8042dc"}, ] [package.dependencies] @@ -858,17 +870,17 @@ python-legacy = ["mkdocstrings-python-legacy (>=0.2.1)"] [[package]] name = "mkdocstrings-python" -version = "1.10.5" +version = "1.10.7" description = "A Python handler for mkdocstrings." optional = false python-versions = ">=3.8" files = [ - {file = "mkdocstrings_python-1.10.5-py3-none-any.whl", hash = "sha256:92e3c588ef1b41151f55281d075de7558dd8092e422cb07a65b18ee2b0863ebb"}, - {file = "mkdocstrings_python-1.10.5.tar.gz", hash = "sha256:acdc2a98cd9d46c7ece508193a16ca03ccabcb67520352b7449f84b57c162bdf"}, + {file = "mkdocstrings_python-1.10.7-py3-none-any.whl", hash = "sha256:8999acb8e2cb6ae5edb844ce1ed6a5fcc14285f85cfd9df374d9a0f0be8a40b6"}, + {file = "mkdocstrings_python-1.10.7.tar.gz", hash = "sha256:bfb5e29acfc69c9177d2b11c18d3127d16e553b8da9bb6d184e428d54795600b"}, ] [package.dependencies] -griffe = ">=0.47" +griffe = ">=0.48" mkdocstrings = ">=0.25" [[package]] @@ -1137,13 +1149,13 @@ files = [ [[package]] name = "pure-eval" -version = "0.2.2" +version = "0.2.3" description = "Safely evaluate AST nodes without side effects" optional = false python-versions = "*" files = [ - {file = "pure_eval-0.2.2-py3-none-any.whl", hash = "sha256:01eaab343580944bc56080ebe0a674b39ec44a945e6d09ba7db3cb8cec289350"}, - {file = "pure_eval-0.2.2.tar.gz", hash = "sha256:2b45320af6dfaa1750f543d714b6d1c520a1688dec6fd24d339063ce0aaa9ac3"}, + {file = "pure_eval-0.2.3-py3-none-any.whl", hash = "sha256:1db8e35b67b3d218d818ae653e27f06c3aa420901fa7b081ca98cbedc874e0d0"}, + {file = "pure_eval-0.2.3.tar.gz", hash = "sha256:5f4e983f40564c576c7c8635ae88db5956bb2229d7e9237d03b3c0b0190eaf42"}, ] [package.extras] @@ -1176,13 +1188,13 @@ windows-terminal = ["colorama (>=0.4.6)"] [[package]] name = "pymdown-extensions" -version = "10.8.1" +version = "10.9" description = "Extension pack for Python Markdown." optional = false python-versions = ">=3.8" files = [ - {file = "pymdown_extensions-10.8.1-py3-none-any.whl", hash = "sha256:f938326115884f48c6059c67377c46cf631c733ef3629b6eed1349989d1b30cb"}, - {file = "pymdown_extensions-10.8.1.tar.gz", hash = "sha256:3ab1db5c9e21728dabf75192d71471f8e50f216627e9a1fa9535ecb0231b9940"}, + {file = "pymdown_extensions-10.9-py3-none-any.whl", hash = "sha256:d323f7e90d83c86113ee78f3fe62fc9dee5f56b54d912660703ea1816fed5626"}, + {file = "pymdown_extensions-10.9.tar.gz", hash = "sha256:6ff740bcd99ec4172a938970d42b96128bdc9d4b9bcad72494f29921dc69b753"}, ] [package.dependencies] @@ -1194,13 +1206,13 @@ extra = ["pygments (>=2.12)"] [[package]] name = "pytest" -version = "8.2.2" +version = "8.3.2" description = "pytest: simple powerful testing with Python" optional = false python-versions = ">=3.8" files = [ - {file = "pytest-8.2.2-py3-none-any.whl", hash = "sha256:c434598117762e2bd304e526244f67bf66bbd7b5d6cf22138be51ff661980343"}, - {file = "pytest-8.2.2.tar.gz", hash = "sha256:de4bb8104e201939ccdc688b27a89a7be2079b22e2bd2b07f806b6ba71117977"}, + {file = "pytest-8.3.2-py3-none-any.whl", hash = "sha256:4ba08f9ae7dcf84ded419494d229b48d0903ea6407b030eaec46df5e6a73bba5"}, + {file = "pytest-8.3.2.tar.gz", hash = "sha256:c132345d12ce551242c87269de812483f5bcc87cdbb4722e48487ba194f9fdce"}, ] [package.dependencies] @@ -1208,7 +1220,7 @@ colorama = {version = "*", markers = "sys_platform == \"win32\""} exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} iniconfig = "*" packaging = "*" -pluggy = ">=1.5,<2.0" +pluggy = ">=1.5,<2" tomli = {version = ">=1", markers = "python_version < \"3.11\""} [package.extras] @@ -1648,4 +1660,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "a80ef76e1e7f0f8c83159a6a97f403bf095f7bf1f8d7d88e9f183b47d35778f3" +content-hash = "a4b9fbde186fdee1988445bc7da1716371448286b80dc13eee51ab4ac103d44d" diff --git a/pyproject.toml b/pyproject.toml index 772cc03..7f4b24a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "jupyter-tikz" -version = "0.4.2" +version = "0.5" description = "IPython Magics for rendering TeX/TikZ in Jupyter Notebooks" license = "MIT" authors = ["lucaslrodri"] @@ -32,11 +32,11 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.dependencies] python = "^3.10" +jinja2= "^3" ipython = "*" [tool.poetry.group.dev.dependencies] pytest = "^8.2.2" -jinja2 = "^3.1.4" isort = "^5.13.2" black = "^24.4.2" taskipy = "^1.13.0" diff --git a/tests/conftest.py b/tests/conftest.py index 5547f8d..3f56417 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -49,9 +49,10 @@ \documentclass[tikz]{standalone} \begin{document} \begin{tikzpicture} - {% for person in people %} - \node[draw] at (0,{{ person.y }}) {Hello, {{ person.name }}!}; - {% endfor %} + (~ A Jinja Template Commentary ~) + (** for person in people **) + \node[draw] at (0,(* person.y *)) {Hello, (* person.name *)!}; + (** endfor **) \end{tikzpicture} \end{document}""" diff --git a/tests/test_args.py b/tests/test_args.py index d7123f7..7814fc9 100644 --- a/tests/test_args.py +++ b/tests/test_args.py @@ -62,13 +62,13 @@ }, ), ( - "use-jinja", - ("-j", "--use-jinja"), + "disable-jinja", + ("-dj", "--disable-jinja"), { - "dest": "use_jinja", + "dest": "disable_jinja", "action": "store_true", "default": False, - "help": "Render the code using Jinja2.", + "help": "Disable Jinja2 rendering.", }, ), ( diff --git a/tests/test_jinja.py b/tests/test_jinja.py index 05b980d..2b582ef 100644 --- a/tests/test_jinja.py +++ b/tests/test_jinja.py @@ -1,10 +1,11 @@ import pytest +from IPython.display import SVG from jupyter_tikz import TexDocument -from jupyter_tikz.jupyter_tikz import _JINJA_NOT_INTALLED_ERR, _NS_NOT_PROVIDED_ERR +from tests.conftest import * EXAMPLE_TIKZ_JINJA_TEMPLATE = """\\begin{tikzpicture} - \\node[draw] at (0,0) {Hello, {{ name }}!}; + \\node[draw] at (0,0) {Hello, (* name *)!}; \\end{tikzpicture} """ @@ -15,35 +16,47 @@ COMPLEX_TIKZ_JINJA_TEMPLATE = r""" \begin{tikzpicture} - {% for person in people %} - \node[draw] at (0,{{ person.y }}) {Hello, {{ person.name }}!}; - {% endfor %} + (** for person in people -**) + \node[draw] at (0,(* person.y *)) {Hello, (* person.name *)!}; + (** endfor **) \end{tikzpicture} """ -def test_init_with_jinja_without_ns_raises_error(): +def test_disable_jinja__render_jinja_not_called(mocker): + # Arrange + spy = mocker.spy(TexDocument, "_render_jinja") + + # Act + TexDocument(EXAMPLE_GOOD_TEX, disable_jinja=True) + + # Assert + assert not spy.assert_not_called() + + +@pytest.mark.needs_latex +@pytest.mark.needs_pdftocairo +def test_disable_jinja__render_correctly(tmpdir, monkeypatch): + # Arrange + monkeypatch.chdir(tmpdir) + tex_document = TexDocument(EXAMPLE_GOOD_TEX, disable_jinja=True) + # Act - with pytest.raises(ValueError) as err: - TexDocument(EXAMPLE_TIKZ_JINJA_TEMPLATE, use_jinja=True) + res = tex_document.run_latex() # Assert - assert _NS_NOT_PROVIDED_ERR in str(err.value) + assert isinstance(res, SVG) -def test_jinja_when_jinja_is_not_installed_raises_error(monkeypatch): +def test__render_jinja_called_by_default(mocker): # Arrange - monkeypatch.setattr("sys.modules", {"jinja2": None}) + spy = mocker.spy(TexDocument, "_render_jinja") # Act - with pytest.raises(ImportError) as err: - res = TexDocument( - EXAMPLE_TIKZ_JINJA_TEMPLATE, use_jinja=True, ns={"name": "World"} - ) - # Assert - assert res is None + TexDocument(EXAMPLE_GOOD_TEX) - assert _JINJA_NOT_INTALLED_ERR in str(err.value) + # Assert + assert not spy.assert_called_once() def test_jinja_using_dict_ns(): @@ -51,9 +64,7 @@ def test_jinja_using_dict_ns(): name = "World" # Act - tex_document = TexDocument( - EXAMPLE_TIKZ_JINJA_TEMPLATE, use_jinja=True, ns={"name": name} - ) + tex_document = TexDocument(EXAMPLE_TIKZ_JINJA_TEMPLATE, ns={"name": name}) # Assert assert str(tex_document).strip() == EXAMPLE_TIKZ_RENDERED_TEMPLATE.strip() @@ -65,7 +76,7 @@ def test_jinja_using_local_ns(): _ = name # Act - tex_document = TexDocument(EXAMPLE_TIKZ_JINJA_TEMPLATE, use_jinja=True, ns=locals()) + tex_document = TexDocument(EXAMPLE_TIKZ_JINJA_TEMPLATE, ns=locals()) # Assert assert str(tex_document).strip() == EXAMPLE_TIKZ_RENDERED_TEMPLATE.strip() @@ -80,9 +91,7 @@ def test_jinja_with_complex_template(): ] # Act - tex_document = TexDocument( - COMPLEX_TIKZ_JINJA_TEMPLATE, use_jinja=True, ns={"people": people} - ) + tex_document = TexDocument(COMPLEX_TIKZ_JINJA_TEMPLATE, ns={"people": people}) # Assert res = f"{tex_document}" @@ -98,20 +107,18 @@ def test_jinja_with_complex_template(): EXAMPLE_TIKZ_JINJA_PARENT_TEMPLATE = """\\begin{tikzpicture} \\draw (-2.5,-2.5) rectangle (5,5); - {% block content %}{% endblock %} + (** block content **)(** endblock **) \\end{tikzpicture} """ -EXAMPLE_TIKZ_JINJA_CHILD_TEMPLATE = """{% extends 'parent_tmpl.tex' %} -{% block content %} - \node[draw] at (0,0) {Hello, {{ name }}!}; -{% endblock %} +EXAMPLE_TIKZ_JINJA_CHILD_TEMPLATE = """(** extends 'parent_tmpl.tex' **) + (** block content -**) + \\node[draw] at (0,0) {Hello, (* name *)!}; + (**- endblock **) """ EXAMPLE_TIKZ_JINJA_EXTENDED_TEMPLATE = """\\begin{tikzpicture} \\draw (-2.5,-2.5) rectangle (5,5); - - \node[draw] at (0,0) {Hello, World!}; - + \\node[draw] at (0,0) {Hello, World!}; \\end{tikzpicture} """ @@ -127,9 +134,7 @@ def test_jinja_extends_template(tmpdir, monkeypatch): _ = name # Act - tex_document = TexDocument( - EXAMPLE_TIKZ_JINJA_CHILD_TEMPLATE, use_jinja=True, ns=locals() - ) + tex_document = TexDocument(EXAMPLE_TIKZ_JINJA_CHILD_TEMPLATE, ns=locals()) # Assert assert f"{tex_document}".strip() == EXAMPLE_TIKZ_JINJA_EXTENDED_TEMPLATE.strip() diff --git a/tests/test_magic_params.py b/tests/test_magic_params.py index aee2aba..9e32d46 100644 --- a/tests/test_magic_params.py +++ b/tests/test_magic_params.py @@ -58,7 +58,7 @@ def test_show_help_on_empy_code(tikz_magic, capsys): EXAMPLE_TIKZ_JINJA_TEMPLATE = """\\begin{tikzpicture} - \\node[draw] at (0,0) {Hello, {{ name }}!}; + \\node[draw] at (0,0) {Hello, (* name *)!}; \\end{tikzpicture} """ @@ -286,20 +286,6 @@ def test_alternative_tex_obj_type(tikz_magic_mock, params, expected_input_type): assert tikz_magic_mock.input_type == expected_input_type -def test_as_jinja_same_as_use_jinja(tikz_magic_mock, monkeypatch): - # Arrange - line = "--as-jinja" - code = "any code" - - monkeypatch.setattr(TexDocument, "__init__", lambda *args, **kwargs: None) - - # Act - tikz_magic_mock.tikz(line, code) - - # Assert - assert tikz_magic_mock.args.use_jinja is True - - # =================== Test src content =================== def test_src_is_cell_content(tikz_magic_mock): # Arrange diff --git a/tests/test_run_latex.py b/tests/test_run_latex.py index fcb64a6..b54372b 100644 --- a/tests/test_run_latex.py +++ b/tests/test_run_latex.py @@ -700,9 +700,7 @@ def test_jinja_template(monkeypatch, tmp_path): ] # Act - tex_document = TexDocument( - EXAMPLE_JINJA_TEMPLATE, use_jinja=True, ns={"people": people} - ) + tex_document = TexDocument(EXAMPLE_JINJA_TEMPLATE, ns={"people": people}) res = tex_document.run_latex() # Assert @@ -714,14 +712,14 @@ def test_jinja_template(monkeypatch, tmp_path): \begin{document} \begin{tikzpicture} \draw (-2.5,-2.5) rectangle (5,5); - {% block content %}{% endblock %} + (** block content **)(** endblock **) \end{tikzpicture} \end{document}""" -EXAMPLE_JINJA_CHILD_TEMPLATE = """{% extends 'parent_tmpl.tex' %} -{% block content %} - \node[draw] at (0,0) {Hello, {{ name }}!}; -{% endblock %} +EXAMPLE_JINJA_CHILD_TEMPLATE = """(** extends 'parent_tmpl.tex' **) +(** block content **) + \node[draw] at (0,0) {Hello, (* name *)!}; +(** endblock **) """ @@ -735,9 +733,7 @@ def test_jinja_extends_template(tmpdir, monkeypatch): parent.write(parent_code) # Act - tex_document = TexDocument( - EXAMPLE_JINJA_CHILD_TEMPLATE, use_jinja=True, ns={"name": "World"} - ) + tex_document = TexDocument(EXAMPLE_JINJA_CHILD_TEMPLATE, ns={"name": "World"}) res = tex_document.run_latex() # Assert diff --git a/tests/test_tex__repr__.py b/tests/test_tex__repr__.py index 788b121..360d395 100644 --- a/tests/test_tex__repr__.py +++ b/tests/test_tex__repr__.py @@ -46,17 +46,17 @@ def test_get_arg_head(code, expected_result): ), ( "TexDocument", - {"ns": {"name": "World"}, "use_jinja": True}, - "TexDocument('\\\\node at(0,0) {};\\n\\\\draw (1,0) -- (2,0) -- (2,1) -- (1,1) -- ...', use_jinja=True)", + {"ns": {"name": "World"}, "disable_jinja": True}, + "TexDocument('\\\\node at(0,0) {};\\n\\\\draw (1,0) -- (2,0) -- (2,1) -- (1,1) -- ...', disable_jinja=True)", ), ( "TexFragment", { "ns": {"name": "World"}, - "use_jinja": True, + "disable_jinja": True, "preamble": "custom preamble", }, - "TexFragment('\\\\node at(0,0) {};\\n\\\\draw (1,0) -- (2,0) -- (2,1) -- (1,1) -- ...', template='standalone-document', preamble='custom preamble', use_jinja=True)", + "TexFragment('\\\\node at(0,0) {};\\n\\\\draw (1,0) -- (2,0) -- (2,1) -- (1,1) -- ...', template='standalone-document', preamble='custom preamble', disable_jinja=True)", ), ( "TexFragment",