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

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
U
Unit 42
Vercel News
Vercel News
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
C
Check Point Blog
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
博客园_首页
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
Last Week in AI
Last Week in AI
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
V
Visual Studio Blog
小众软件
小众软件

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.