Airflow override 'ds' within macro function - python-3.x

I'm trying to pass a dynamic date value into a macro function ds_format.
DEFAULT_DATE= '{{ ds }}'
__init__(self, exec_date=DEFAULT_DATE): self.exec_date = exec_date
Now, I've some macros within the functions like
{{macros.ds_format(ds, "%Y-%m-%d", "%Y%m%d")}}
But, I want to pass the exec_date into the macro like
"""{{ macros.ds_format(""" + self.exec_date + """, "%Y-%m-%d", "%d%m%Y") }}"""
Is this the right way to pass a value to to a micro in Airflow (jinja2) ? or. Is there a different way to pass the dynamic value (exec_date) ?.

You need to just pass as follow:
"""{{ macros.ds_format(ds, "%Y-%m-%d", "%d%m%Y") }}"""
An example is https://stackoverflow.com/a/52137676/5691525
Edited:
Why don't you use the following code if you just want to use exectiuon date:
EXEC_DATE = "{{ execution_date.strftime('"%d%m%Y"') }}"
If you want to just use variable why even use jinja at all? Just use normal python datetime that can be seen in this post: How do I turn a python datetime into a string, with readable format date?

Isn't it simpler to just use the {{ ds_nodash }} instead?
https://airflow.apache.org/code.html#macros

Related

Convert h:m:s to hh:mm:ss in twig

I have a little problem that I can't seem to solve and ask for your help please:
I have a branch variable which only contains hour, minute and second with no date. The problem is that its format is h: m: s is I would like to convert it to hh: mm: ss
My variable is : {{hours}} which contains for example: 1:5:7 is i would like to get a result 01:05:07.Can you help me please.
Note that I do not have access to the symfony4 controller because I am a designer thank you in advance.
You can do it using date anyways:
{% set hours = "1:2:3" %}
{{ hours|date('H:i:s') }}
#01:02:03

How to use variable in url?

I'm trying to develop a maco in python. I need a method that changes the URI given in the input variables.
For instance, in www.(variable).com I need to change the URL www.1.com to www.2.com
You can use formatted strings like this:
variable = 1
url = f"www.{variable}.com"
You could store your list of names in a list and then interpolate the variable into the f-string like:
names = ['google', 'amazon', 'microsoft']
for name in names:
print(f"www.{name}.com")
OUTPUT
www.google.com
www.amazon.com
www.microsoft.com

Liquid filter date in Logic App is not working as expected

I have a very simple map written in Liquid and executed in a Logic App.
The map contains only this :
{
"myDate": "{{ "now" }}"
}
The ouput of the map is
{
"myDate": "Now"
}
According to the Liquid documentation it is the right way to generate the current datetime. I tried to put the N in uppercase but it does not work.
Maybe there is a list of supported Liquid features somewhere?
I also tried to apply filters like the date filter in order to change the format of a datetime, but the map is not working as expected.
You have to format the output as this example
{
"Date" : "{{ "now" | Date: "MM/dd/yyyy" }}"
}

Twig RAW filter on literal string not working

{{ (vendorData.description) ? vendorData.description : "<em>No Description Entered</em>"|raw }}
When the value is not present I see:
<em>No Description Entered</em>
Printed literally on the screen in the web browser.
Raw should force the characters to be literal, not > < etc.
Why does this not work on a "created string" but if I do it on a string variable it works?
You need to place brackets around the whole statement like so:
{{ ((vendorData)
? vendorData
: "<em>No Description Entered</em>")|raw }}
Here is a working twigfiddle to show it working:
https://twigfiddle.com/fs2oc2
You can use twigfiddle to experiment with your code.
From feedback in comments section:
here is a twig example to show what you need: https://twigfiddle.com/hjyslr

Setting a key to array from a variable in Twig

I want to substitute a value from a variable as a key to an array. I have that code:
{{ blogPost.getContent()|replace({truncationMark: ''})|raw }}
I've tried the way, but I've encountered with a problem: truncationMark is interpretated as string! It even replaces "truncationMark" substring in blogpost.getContent().
So what I have do to solve the problem?
I'd be thankful for any help.

Resources