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

标签 python cookiecutter

我想为我们的内部项目创建一个模板。

项目的布局包含数量可变的子文件夹。

我想在一些配置文件中指定要创建的子文件夹。 例如,我有以下文件夹结构:

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

配置文件应该是这样的:

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

我尝试创建以下结构:

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

并将提到的配置添加到 cookiecutter.json

我使用 --no-input 运行 cookiecutter。结果只有一个子文件夹 (Table1)。

还尝试使用以下文件夹名称:{% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %} 使用以下配置:

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

输出仍然是单个子文件夹 (Table2Table1Table3)

知道如何实现所需的结构吗?

最佳答案

好吧,最终我写了一个使用 Cookiecutter's package functions 的 python 脚本有两个模板文件夹。

一个模板文件夹用于主项目,一个模板文件夹用于表文件夹。

脚本会使用 cookiecutter.main.cookiecutter 函数创建主项目,并循环创建子文件夹。

我在博客中写了一篇更详细的文章。 不确定这里关于发布个人博客链接的规则。因此,如果以后有人想看它,请在评论中告诉我。

关于python - 如何使用子文件夹模板在 cookiecutter 模板中创建多个子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60025115/

相关文章:

python - Streamlit azure 部署请稍候屏幕错误

python - 指定的模板库无效。尝试加载时引发导入错误 'compressor.templatetags.compress' : cannot import name 'six'

python - Cookiecutter:在以下位置找不到 X 的有效存储库:

python - 基于两列合并两个 Dataframe

python - Ruby 程序员学习 Python 的最快方法是什么?

python - Whoosh NestedChildren 搜索未返回所有结果

Django CMS - cookie cutter : The form could not be loaded. 请检查服务器是否正常运行

python - 使用 docker 的 cookiecutter-django 本地开发

python - Cookiecutter-bonobo 初始化失败

python - Cookiecutter 创建的目录给我运行开发服务器和 python shell 的问题