-
Notifications
You must be signed in to change notification settings - Fork 13.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow list and dict item to be rendered as taget renderer instead of showing as JSON #14024
Conversation
airflow/www/views.py
Outdated
@@ -898,7 +898,7 @@ def rendered_templates(self): | |||
content = getattr(task, template_field) | |||
renderer = task.template_fields_renderers.get(template_field, template_field) | |||
if renderer in renderers: | |||
if isinstance(content, (dict, list)): | |||
if isinstance(content, dict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if isinstance(content, dict): | |
if renderer == "json" and isinstance(content, (dict, list)): |
This will probably be better approach as will solve your problem and preserve the current behavior for fields marked as json
. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a work-around, we can handle it here. But I actually believe the logic should be handled by the renderer itself.
Just make JSON renderer the fallback renderer when no renderer is specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree , If we are also rendering the each dictionary item instead of rending as json.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if somebody using YAML wants this feature too. I think only some generic serialization format is capable of rendering types other than strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO can we extract renderer code for JSON from the view and my be map a custom render function for json here ?
Line 346 in d2efb33
'json': lambda x: render(x, lexers.JsonLexer), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this? Actually I am even thinking whether a field accepts a list or not is also decided by the Operator...
def get_attr_renderer():
"""Return Dictionary containing different Pygments Lexers for Rendering & Highlighting"""
return {
- 'json': lambda x: render(x, lexers.JsonLexer),
+ 'json': lambda x: render(x if isinstance(content, (dict, list)) else json.dumps(x), lexers.JsonLexer),
- 'yaml': lambda x: render(x, lexers.YamlLexer),
+ 'yaml': lambda x: render(x if isinstance(content, (dict, list)) else json.dumps(x), lexers.YamlLexer),
'py': lambda x: render(get_python_source(x), lexers.PythonLexer),
'python_callable': lambda x: render(get_python_source(x), lexers.PythonLexer),
}
python fields are already doing similar transformation.
98f33e2
to
7739b4c
Compare
Hi @turbaszek , I have separated json renderer filtering code from view and created a "json_render()" function and mapped in "wrapped_markdown" function. |
The PR is likely OK to be merged with just subset of tests for default Python and Database versions without running the full matrix of tests, because it does not modify the core of Airflow. If the committers decide that the full tests matrix is needed, they will add the label 'full tests needed'. Then you should rebase to the latest master or amend the last commit of the PR, and push it with --force-with-lease. |
closes: #13988
In #11061 i believe @turbaszek merged code for better rendering dictionary, but code
airflow/airflow/www/views.py
Lines 901 to 903 in d2efb33
also dumps list as json which is passed to render() function as string containing json which is causing this issue.
airflow/airflow/www/utils.py
Lines 308 to 317 in d2efb33
If we remove list from
airflow/airflow/www/views.py
Line 901 in d2efb33
it will pass list to render function and render each list item as target render.
In this case as SQL