Skip to content

Directory walking #2123

Open
Open

Description

@RuofengX

Description

I want use typst to compose type that "3 screenshots per page, landscape, images from a folder".

I want typest to behavior like css but the output is pdf. When I trying to achieve this, there are some missing
future:

  • walk through a folder by given path, return a iterator which yields all file path
  • render a length-unknown image path list

Use Case

Use typst as a universal type set tool.

Activity

  1. added
    feature request
    on Sep 13, 2023
  2. RuofengX commented on Sep 13, 2023

    @RuofengX
    Author
    #set page("a4", flipped: true)
    
    #let cell = rect.with(
      inset: 8pt,
    )
    
    #grid(
      columns: (auto, auto, auto),
      rows: (100%),
      gutter: 8pt,
      cell[#image("./images/1.png")],
      cell[#image("./images/2.png")],
      cell[#image("./images/3.png")],
      cell[#image("./images/4.png")],
      cell[#image("./images/5.png")],
      cell[#image("./images/6.png")],
      cell[#image("./images/7.png")],
    )

    Such code will compose some paper of 7 images on it, 3 per page.
    But I have a folder of many screenshots, maybe 100 pictures.
    Is there some tool(package or module) could handle these scanery?

  3. LuxxxLucy commented on Sep 13, 2023

    @LuxxxLucy
    Contributor

    I recommend try with the preprocessing approach (using "typst-pyimage") first.

  4. changed the title [-]Read all images from a folder and render.[/-] [+]Directory walking[/+] on Sep 13, 2023
  5. added
    scripting
    on Sep 13, 2023
  6. LuxxxLucy commented on Sep 13, 2023

    @LuxxxLucy
    Contributor

    @laurmaedje do we want to implement a function for directory walking?

    Maybe adding a interface named "list_dir" which takes a path and returns an array?

  7. laurmaedje commented on Sep 13, 2023

    @laurmaedje
    Member

    I'm not yet completely decided, but I'm open to discussing it. It should of course be restricted to the project/package directory like file loading. And there are further design decisions about what exactly it returns (what about folders for instance: is there a way to tell whether an array item is a file or folder). And then we need a good and efficient interface for the World trait.

  8. LuxxxLucy commented on Sep 23, 2023

    @LuxxxLucy
    Contributor

    How about a simple design like this (two new function)

    1. list_dir(path: str): returns a list of strings under the given path, which includes files and sub-folders.
    2. is_dir(path: str): returns whether a path is a directory.

    I think this should be a minimum design; if a path is not a directory but a filename, then we should use functions such as "read" (we might also need to add some debug log and throw the error should we read a directory, instead of a file).

  9. drakeaxelrod commented on Oct 26, 2023

    @drakeaxelrod

    My use case for the capability to walk a directory has to do with sorting files based on content and then sorting based on a metric. The requirement for this functionality is for writing a penetration testing report in which I need to walk through a directory and import/include/sort findings based on cvss scores.

  10. reed-smout commented on Jan 22, 2024

    @reed-smout

    Not sure if this is still on the table, but I thought I'd just mention my use case on the off-chance it helps anyone.

    I am designing a recipe book, each recipe is a Yaml file in a sub-dir of the project. My Typst book iterates through the recipes to produce the formatted output. It currently does this with a hard-coded set of paths, but it would be nice to be able to programmatically walk through all the files in that sub-dir that's effectively part of the "source" of the project.

    I did think about making the recipes Typst files instead of Yaml, but that wouldn't change much as there does not seem to be any form of glob importing currently. If there is a neat way of doing this already, I don't know about it sorry.

    Anyway just my two cents, I still think this issue is relevant!

  11. lucifer1004 commented on Jun 12, 2024

    @lucifer1004
    Contributor

    Hi guys, I just made PR #4380 which provides a readdir function. This function returns the names of a directory's contents as an array of strings.

    #readdir("test-folder")

    renders something like below

    ("a.txt", "c", "b.json")

    Thus we can do things like

    #for file in files {
      if file.ends-with(".txt") {
        read("test-folder/" + file)
      }
    }
  12. Doom4535 commented on Dec 2, 2024

    @Doom4535

    I'd also like to have this ability for automatically including the contents of certain folders into an appendix; my current approach is:

    = Appendix
    #show: appendix
    == Matlab Code
    === ber_curves.m
    #let text = read("matlab/ber_curves.m")
    #raw(text, lang: "matlab")
    === Exsample_aliasing.m
    #let text = read("matlab/Exsample_aliasing.m")
    #raw(text, lang: "matlab")
    ...

    I would much rather be able to just point it at the matlab folder and have it add an entry for each file that is present there.

  13. ayoubelmhamdi commented on Mar 21, 2025

    @ayoubelmhamdi

    i think, the glob function is more convenient like other languages:

    #let files = glob("output/*.{png,jpg,jpeg,svg,gif}")
    #let images = files.map(f => image(f))
    
    #grid(
      columns: (1fr,) * 3,
      gutter: 1em,
      ..images
    )
  14. terefang commented on Apr 8, 2025

    @terefang
  15. bratorange commented on May 14, 2025

    @bratorange

    I want to add that such a feature would be very much appreciated! I often had the case where I wanted to load data like result01.csv, result02.csv, ... and plot it, but had to write unconvenient solutions for that.

  16. mewmew commented on Jul 11, 2025

    @mewmew
    Contributor

    And then we need a good and efficient interface for the World trait.

    As I'm still getting to know the ins and outs of Typst. Is the World trait the basic requirement used so that typst watch knows when to recompile the project as assets change?

    For a directory walking/glob API, being able to track when files resolved in such a way are updated would be needed to maintain the incredible dependency graph that typst watch is so beautifully able to track.

  17. laurmaedje commented on Jul 21, 2025

    @laurmaedje
    Member

    As I'm still getting to know the ins and outs of Typst. Is the World trait the basic requirement used so that typst watch knows when to recompile the project as assets change?

    Essentially yes. As long as the stuff returned by the World trait has the same hash, things are cached.

  18. KronosTheLate commented on Apr 5, 2026

    @KronosTheLate
    Contributor

    Just want to chime in and say that I would love this feature.

    My use case would be to create a cookbook, where each recipe would be a separate file. I can then section the recipes by folders such as "Desserts", "Dinners", "Bakery" etc, and just include all files in that directory.

  19. skeletony007 commented on May 1, 2026

    @skeletony007

    I too would like this feature.

    So much so that, for Git repositories, I've already written a quick plugin that can walk tree objects:

    #import "@local/permagit:0.1.0" as git
    #let r(path) = read(".git/" + path, encoding: none)
    #git.tree.parse-array-recursive(r, git.tree.read(r, "refs/heads/master")).map(path => { 
      page({
        heading([Blob: #raw(path)])
        raw(str(git.blob.read(r, "refs/heads/master", path)))
      })
    }).join()
  20. Jollywatt commented on May 23, 2026

    @Jollywatt
    Contributor

    As Typst rapidly becomes more suited for building blogs with HTML + bundle export, the ability to list files (e.g. blog posts) in a folder becomes more useful.

  21. Myriade-Medieval commented on Jul 15, 2026

    @Myriade-Medieval

    While we wait for this feature to be thought out and implemented, here is my workaround:
    Create an index file that lists all files in your directory, find is a good fit for it (find -type f > index)
    Then in your typst code you can iterate over the lines of index with read:

    #for file in read("index").split("\n").filter(x => x != "") {
      read(file) // or whatever you want to do with the file
    }

    I have my find command in a make-index.sh file, which I run once in a while. It's not completely automated, but it works well enough for my use case

  22. bastienvoirin commented on Jul 17, 2026

    @bastienvoirin
    SponsorContributor

    Or simply

    #for file in read("index").trim("\n").split("\n") {
      read(file) // or whatever you want to do with the file
    }

    :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestscripting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions