From fafea87b7d8d6a55de16a9419a352eb05c99a088 Mon Sep 17 00:00:00 2001 From: Estelle Poulin Date: Wed, 14 Jun 2023 17:44:42 -0400 Subject: [PATCH] Readme and model fixes --- README.md | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ rtex/__init__.py | 2 +- rtex/client.py | 6 ++++ rtex/models.py | 2 +- 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..0a5e334 --- /dev/null +++ b/README.md @@ -0,0 +1,88 @@ +# Async Python Client to The RTEX API Server + +## Installation + +```bash +pip install rtex +``` + +## Usage + +The API surface of Rtex is spartan so this is basically the whole thing. + +```python +import asyncio + +from rtex.client import AsyncRtexClient + +async def amain(): + async with AsyncRtexClient() as rtex: + res = rtex.create_render("\(x^2 + x - 1\)") + + if res.status == "success": + with open("equation.png") as output_fd: + await res.save_render( + res.filename, + output_fd + ) + +def main(): + asyncio.run(amain()) + +if __name__ == "__main__": + main() +``` + +## No Thoughts, Just Render + +```python +async def amain(): + async with AsyncRtexClient() as rtex: + buf = await rtex.render_math("e^x + 1") + + # `buf` now contains the bytes of the PNG +``` + +## Do I look like I know what a Jay-Peg is? + +```python +async def amain(): + async with AsyncRtexClient() as rtex: + # The render methods accept a format parameter. + # Supported values are "png", "jpg" and "pdf" + buf = await rtex.render_math("e^x + 1", format="jpg") +``` + +## Self-Hoster + +Set the environment variable `RTEX_API_HOST` or do the following. + +```python + +async def amain(): + async with AsyncRtexClient(api_host="https://myserver.ru") as rtex: + buf = await rtex.render_math("e^x + 1") +``` + + +## I Can Tell By The Pixels + +`quality` in Rtex speak is an abstract notion of compression for the given +format where `100` is the least compressed and `0` is the most. At the time of +writing the default is `85`. + +`density` in Rtex speak is how much to sample the rendered PDF when generating +an image. This has no effect on the `"pdf"` format. At the time of writing the +default is `200`. + +```python + +async def amain(): + async with AsyncRtexClient(api_host="https://myserver.ru") as rtex: + needs_more_jpeg = await rtex.render_math( + "e^x + 1", + density=50, + quality=1 + ) +``` + diff --git a/rtex/__init__.py b/rtex/__init__.py index f102a9c..3b93d0b 100644 --- a/rtex/__init__.py +++ b/rtex/__init__.py @@ -1 +1 @@ -__version__ = "0.0.1" +__version__ = "0.0.2" diff --git a/rtex/client.py b/rtex/client.py index 56bfe27..89c806f 100644 --- a/rtex/client.py +++ b/rtex/client.py @@ -144,12 +144,18 @@ class AsyncRtexClient: self, code: str, format: RenderFormat = "png", + documentclass: str = "minimal", + quality: Optional[RenderQuality] = None, + density: Optional[RenderDensity] = None, ): final_doc = rf"\({code}\)" res = await self.create_render( code=final_doc, format=format, + documentclass=documentclass, + quality=quality, + density=density, ) if res.status == "error": diff --git a/rtex/models.py b/rtex/models.py index 1aaae4d..a3f91d8 100644 --- a/rtex/models.py +++ b/rtex/models.py @@ -3,7 +3,7 @@ from typing import Annotated, Literal, Optional, Union from pydantic import BaseModel, Field, conint RenderFormat = Literal["png", "jpg", "pdf"] -RenderQuality = Annotated[int, conint(ge=0, le=100)] +RenderQuality = Annotated[int, conint(gt=0, le=100)] RenderDensity = int