sphinx-contrib/datatemplates

Support for multiple data files for a single template

oharboe opened this issue · 11 comments

@David-Cowan Sometimes it's useful to pull multiple data files together.

Is there any interest in adding such a feature?

Maybe something like:

.. datatemplate:yaml:: file1.yaml file2.yaml
   {{data[0].foo}}
   {{data[1].bar}}

Nice idea, I can see how that would be useful.

I worry a bit that someone might want to combine a JSON file and a YAML file and use them both with a template, so we would need to think about whether we want to support that and if so how the user would express the formats for the different inputs.

I considered suggesting that we leave the data argument alone and add a data_source template argument that is a map from filename to data structure. After thinking about it more, I think it does make sense to just change data for this case. I don't know whether a sequence (as you propose) or map would work better. The sequence involves less typing, I guess, and how many files is someone actually likely to try to combine?

OK, let's not worry too much about the multiple format angle, for now.

It would help to have some small but specific examples of how you might want to join 2 files together, to work out a way to express that. For example, if the top level structure of the file is a map, then joining them with .update() would make sense and we would just need to set a precedence order to explain what happens when keys collide. If the 2 files are both lists, .extend() would work. But if one file is a map and one is a list, then the approach @oharboe suggested where the template refers explicitly to each file in some way would be safer. The template author knows the content structure, after all.

Maybe instead of extending the directive syntax, we should expose the loader functions. Then a template author could use the set statement to load data into a variable and use it however they want.

https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments

Maybe instead of extending the directive syntax, we should expose the loader functions. Then a template author could use the set statement to load data into a variable and use it however they want.

https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments

Makes sense. Example syntax?

See what you think of #81 as an approach.

The limit would likely be RAM, since all of the data is loaded into memory immediately. That's the case for all of the formats currently supported.