惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
IT之家
IT之家
博客园 - 聂微东
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
小众软件
小众软件
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
量子位
月光博客
月光博客
博客园 - 【当耐特】
博客园 - 叶小钗

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers Python Reverse a List New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode
Using Custom Delimiters in Jinja Templates
2022-02-18 · via Stonecharioteer on Tech

Apache Airflow uses Jinja templates for variable expansion and templating. Whenever you write a DAG, you can use {{ }} blocks to expand variable names, often using Airflow configurations or macros to fill in the values. While that is great, you get a problem when you have Jinja templates inside your code that has nothing to do with Airflow.

At those times, you should probably note that Jinja supports custom variable start and stop markers.

Standard Jinja Syntax

1
2
3
4
5
6
7
template_string = """Select * from {{bigquery_project}}.{{bigquery_dataset}}.{{bigquery_table}}"""
template = jinja2.Template(template_string)
rendered_string = template.render(
   bigquery_project="my_gcp_project",
   bigquery_dataset="my_gcp_dataset",
   bigquery_table="my_gcp_table"
)

This uses the normal markers that Jinja uses, just like many other templating languages. However, you might want to use other markers if you’re using this code block within the context of an Airflow DAG.

Then, you’d use:

1
2
3
4
5
6
7
template_string = """Select * from [[bigquery_project]].[[bigquery_dataset]].[[bigquery_table]]"""
template = jinja2.Template(template_string, variable_start_string="[[", variable_end_string="]]")
rendered_string = template.render(
   bigquery_project="my_gcp_project",
   bigquery_dataset="my_gcp_dataset",
   bigquery_table="my_gcp_table"
)

If you’d like to know more about this, you should look at the official Jinja2 documentation on the Template class.

Note

You can use these to override the need to implement your own hacks for nested template variables. This will even help with Flask or Django templates. Note that you probably shouldn’t implement too much of your logic within a template, but it is useful to know that this exists.

Warning

Another way of doing this is to use jinja2.Undefined so that Template.Render ignores your undefined values, so that the values can be filled in later.

While this works, you shouldn’t use something that’s implemented in order to help you debug code, and you should instead use the provided APIs to do this.