Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow compile function to take a string as input #4

Closed
slashformotion opened this issue May 6, 2023 · 8 comments · Fixed by #89
Closed

Allow compile function to take a string as input #4

slashformotion opened this issue May 6, 2023 · 8 comments · Fixed by #89
Labels
enhancement New feature or request

Comments

@slashformotion
Copy link

Would it be possible to have a compile function that take a string as a parameter ?

@messense messense changed the title Feature request: Allow compile function to take a string as input May 9, 2023
@ok-nick
Copy link

ok-nick commented Jun 14, 2023

I need to generate tens of thousands of individual pdfs, all that reference a unique path. To do that, I generate a .typ file that imports my template and calls it with the unique paths as parameters. The problem is that having to generate a temporary file per pdf is extremely slow, especially at such a large scale. It would be much more performant to skip this step entirely and pass a string or byte array directly to typst.

My use case is a workaround to this issue: typst/typst#295

@ntjess

This comment has been minimized.

@messense

This comment has been minimized.

@ntjess

This comment has been minimized.

@JCGoran
Copy link

JCGoran commented Sep 17, 2023

This seems like a very handy feature, and it would also make the package more "Pythonic" IMO. By more "Pythonic", consider for instance the built-in json module; the json.load function loads from a stream:

with open(file, 'r') as f:
    data = json.load(f)

while the json.loads function loads it from a string, which should be valid JSON:

data = json.load([VALID_JSON_STRING])

Making typst.compile work the same way would therefore bring it inline with the interface of the standard lib.
However, from reading a bit through the codebase(s), it seems (from my limited understanding at least) that this would need to be implemented at the level of the typst library/compiler itself, not this wrapper, as the Rust compile method takes a file as input.
Note that typst projects can involve multiple files, making this a bit complex to implement in full generality.

@ok-nick regarding your particular issue, have you considered using a RAM disk/temp filesystem as a workaround? If on Linux, mounting a dir as a tmpfs can potentially lead to significant performance gains (just make sure all of the files you are using for I/O are exclusively in that dir).

@ok-nick
Copy link

ok-nick commented Sep 17, 2023

That'd be a good workaround for now, I'll have to try it out.

@66Leo66
Copy link

66Leo66 commented Sep 17, 2024

I think this feature request is really useful for automation/scripting tasks that need to generate a (maybe simple) document without messing with temp files. typst-cli already accepts compile from stdin: typst/typst#3339. It would be cool if similar approach could be taken so that typst-py could support working with "fake files" (as added in the mentioned PR).

@messense
Copy link
Owner

Pull requests are welcome.

@messense messense added the enhancement New feature or request label Sep 17, 2024
@messense messense marked this as a duplicate of #88 Mar 6, 2025
@messense messense linked a pull request Mar 6, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants