Skip to content
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

docs: jinja #15019

Merged
merged 6 commits into from Jun 9, 2021
Merged

docs: jinja #15019

merged 6 commits into from Jun 9, 2021

Conversation

srinify
Copy link
Contributor

@srinify srinify commented Jun 7, 2021

Adding some basic documentation on Jinja templating and how to use it!

@request-info
Copy link

request-info bot commented Jun 7, 2021

We would appreciate it if you could provide us with more info about this issue/pr! Please do not leave the title or description empty.

@request-info request-info bot added the need:more-info Requires more information from author label Jun 7, 2021
@srinify
Copy link
Contributor Author

srinify commented Jun 7, 2021

Screen Shot 2021-06-07 at 11 14 12 AM

Screen Shot 2021-06-07 at 11 14 08 AM

Screen Shot 2021-06-07 at 11 14 03 AM

@srinify srinify changed the title docs/jinja_macros docs/jinjamacros Jun 7, 2021
@srinify srinify changed the title docs/jinjamacros docs: jinja Jun 7, 2021
Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on, this was long overdue! The section on cache_key_wrapper could be more precise. In addition, a description needs to be added to the PR.

Comment on lines 137 to 146
**Skipping Cache**

If you have enabled caching, you will need to chain the `{{ cache_key_wrapper() }}`
function to avoid retrieving the
same current_username or current_user_id for different users.

Here's an example:

- Skip cache when retrieving current user_id: `{{ cache_key_wrapper(current_user_id()) }}`
- Skip cache when retrieving current username: `{{ cache_key_wrapper(current_username()) }}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_key_wrapper is nowadays applied by default to current_user_id and current_username (it can be disabled by calling them with add_to_cache_keys=False). See here for more context:

def current_user_id(self, add_to_cache_keys: bool = True) -> Optional[int]:
"""
Return the user ID of the user who is currently logged in.
:param add_to_cache_keys: Whether the value should be included in the cache key
:returns: The user ID
"""
if hasattr(g, "user") and g.user:
if add_to_cache_keys:
self.cache_key_wrapper(g.user.get_id())
return g.user.get_id()
return None
def current_username(self, add_to_cache_keys: bool = True) -> Optional[str]:
"""
Return the username of the user who is currently logged in.
:param add_to_cache_keys: Whether the value should be included in the cache key
:returns: The username
"""
if g.user and hasattr(g.user, "username"):
if add_to_cache_keys:
self.cache_key_wrapper(g.user.username)
return g.user.username
return None

cache_key_wrapper is therefore only needed when it's necessary to wrap your own custom function return values into the cache key. See here for more context:

def cache_key_wrapper(self, key: Any) -> Any:
"""
Adds values to a list that is added to the query object used for calculating a
cache key.
This is needed if the following applies:
- Caching is enabled
- The query is dynamically generated using a jinja template
- A `JINJA_CONTEXT_ADDONS` or similar is used as a filter in the query
:param key: Any value that should be considered when calculating the cache key
:return: the original value ``key`` passed to the function
"""
if self.extra_cache_keys is not None:
self.extra_cache_keys.append(key)
return key

@srinify srinify requested a review from villebro June 9, 2021 13:56
Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work!

@srinify srinify merged commit e82433e into apache:master Jun 9, 2021
cccs-RyanS pushed a commit to CybercentreCanada/superset that referenced this pull request Dec 17, 2021
* added Jinja macro documentation

* fixed typo!

* Update docs/src/pages/docs/installation/sql_templating.mdx

Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>

* incorporated Villes feedback on cache keys

* fixed section title

Co-authored-by: Evan Rusackas <evan@preset.io>
Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>
QAlexBall pushed a commit to QAlexBall/superset that referenced this pull request Dec 29, 2021
* added Jinja macro documentation

* fixed typo!

* Update docs/src/pages/docs/installation/sql_templating.mdx

Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>

* incorporated Villes feedback on cache keys

* fixed section title

Co-authored-by: Evan Rusackas <evan@preset.io>
Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
* added Jinja macro documentation

* fixed typo!

* Update docs/src/pages/docs/installation/sql_templating.mdx

Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>

* incorporated Villes feedback on cache keys

* fixed section title

Co-authored-by: Evan Rusackas <evan@preset.io>
Co-authored-by: Kamil Gabryjelski <kamil.gabryjelski@gmail.com>
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.3.0 labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels need:more-info Requires more information from author preset-io size/L 🚢 1.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants