简体   繁体   English

如何使用子文件夹模板在 cookiecutter 模板中创建多个子文件夹

[英]How to create multiple sub-folders in cookiecutter template using a sub-folder template

I want to create a template for our internal projects.我想为我们的内部项目创建一个模板。

The layout of the project contains a variable amount of sub-folders.项目的布局包含可变数量的子文件夹。

I would like to specify the sub-folders to create, inside some configuration file.我想在一些配置文件中指定要创建的子文件夹。 For example, I have the following folder structure:例如,我有以下文件夹结构:

my_project
|
|___ Tables
|  |
|  |__ Table1    <--- The folders I would like to create
|  |__ Table2    <---|
|  |__ Table3    <---|
|
|___ Other_folders

The configuration file should have something like this:配置文件应该是这样的:

{
  "prj_name": "my_project",
  "tables": ["Table1", "Table2", "Table3"]
}

I have tried to create the following structure:我试图创建以下结构:

{{cookiecutter.prj_name}}
|
|___ Tables
|  |
|  |__ {% for table in cookiecutter.tables %}{{table}}{% endfor %}
|
|___ Other_folders

and added to cookiecutter.json the mentioned configuration.并添加到cookiecutter.json提到的配置。

I ran cookiecutter using --no-input .我使用--no-input运行 cookiecutter。 And the result was only a single sub-folder (Table1).结果只有一个子文件夹(表1)。

Also tried to use the following folder name: {% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %} With the following configuration:还尝试使用以下文件夹名称: {% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %}使用以下配置:

{
  "prj_name": "my_project",
  "tables": {
    "table1": "table1",
    "table2": "table2",
    "table3": "table3"
  }
}

The output was still a single sub-folder (Table2Table1Table3)输出仍然是单个子文件夹 (Table2Table1Table3)

Any idea how I can achieve the desired structure?知道如何实现所需的结构吗?

Well eventually I wrote a python script that uses Cookiecutter's package functions with two template folders.好吧,最终我写了一个 python 脚本,它使用Cookiecutter 的包函数和两个模板文件夹。

One template folder for the main project, and one template folder for the tables folders.一个用于主项目的模板文件夹,一个用于表文件夹的模板文件夹。

And the script would use cookiecutter.main.cookiecutter function to create the main project, and the sub folders in a loop.并且脚本将使用cookiecutter.main.cookiecutter函数来创建主项目,并在循环中创建子文件夹。

I've wrote a more detailed post in a blog.我在博客中写了一篇更详细的文章。 Not sure about the rules here about posting links to personal blogs.不确定这里关于发布个人博客链接的规则。 So if someone in the future would like to see it, let me know in the comments.因此,如果将来有人想看它,请在评论中告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不指定子文件夹名称的情况下自动合并来自多个子文件夹的多个 CSV? - How to automatically merge multiple CSVs from multiple sub-folders, without specifying sub-folder name? 递归地将子文件夹中的文件读入列表,并将每个子文件夹的文件合并为每个子文件夹一个csv - Recursively read files from sub-folders into a list and merge each sub-folder's files into one csv per sub-folder 如何从文件夹(包括子文件夹)的图像创建 Numpy 数组 - How to create Numpy Arrays from images of a folder including sub-folders 如何编译位于带有子文件夹的文件夹中的 Python 软件 - How to compile my Python software that's in a folder with sub-folders 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 如何从多个子文件夹中随机选择 select 文件 - How to select file randomly from multiple sub-folders 如何让Python查看子文件夹? - How to get Python to look at Sub-Folders? 使用python在多个不同的文件夹中创建类似的多个子文件夹 - Creating similar multiple sub-folders in multiple different folders using python 训练具有多个文件夹和子文件夹的 CNN 模型 - Train CNN model with multiple folders and sub-folders 如何提取子文件夹路径? - How to extract sub-folder path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM