Skip to content

Client

TexRenderingClient dataclass

Source code in src/tex2image/client.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@dataclass(frozen=True)
class TexRenderingClient:
    host: str = "localhost"
    port: int = 8000

    @property
    def base_url(self) -> str:
        return f"http://{self.host}:{self.port}"

    def request_latex_to_png(
        self,
        latex_snippet: str,
        template: str | None = None,
    ) -> bytes:
        """Convert a LaTeX snippet to a PNG image, returning the image bytes.

        See
        [the documentation for `render_latex_snippet`](https://tex2image.readthedocs.io/en/latest/renderer#tex2image.rendering.render_latex_to_png)
        for more information on the parameters.
        """
        response = requests.get(
            f"{self.base_url}/render_latex_snippet",
            params={"latex_snippet": latex_snippet, "template": template},
        )
        response.raise_for_status()
        return response.content

request_latex_to_png(latex_snippet, template=None)

Convert a LaTeX snippet to a PNG image, returning the image bytes.

See the documentation for render_latex_snippet for more information on the parameters.

Source code in src/tex2image/client.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def request_latex_to_png(
    self,
    latex_snippet: str,
    template: str | None = None,
) -> bytes:
    """Convert a LaTeX snippet to a PNG image, returning the image bytes.

    See
    [the documentation for `render_latex_snippet`](https://tex2image.readthedocs.io/en/latest/renderer#tex2image.rendering.render_latex_to_png)
    for more information on the parameters.
    """
    response = requests.get(
        f"{self.base_url}/render_latex_snippet",
        params={"latex_snippet": latex_snippet, "template": template},
    )
    response.raise_for_status()
    return response.content