Skip to content

Let favorite queries use Jinja2 templates - #2085

Merged
rolandwalker merged 1 commit into
mainfrom
RW/jinja2-favorite-queries-optional-parameters
Aug 4, 2026
Merged

Let favorite queries use Jinja2 templates#2085
rolandwalker merged 1 commit into
mainfrom
RW/jinja2-favorite-queries-optional-parameters

Conversation

@rolandwalker

@rolandwalker rolandwalker commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Description

Let favorite queries use Jinja2 templates, allowing optional arguments and control blocks in favorite queries.

Include completions for Jinja2 key names. Supporting the completions is quite a bit more code than the core functionality! Completions also can only be offered if the key name is referred to explicitly in the template (not merely iterated over, which is technically possible).

Extensively update the config file and internal helpdoc with examples and notes.

It seems arbitrary to choose whether Jinja2 templates are processed before or after the legacy positional arguments are substituted. I chose Jinja2 before positional. The agent hacked together a UUID-based solution for this, which may not be pretty.

There is one regression: whereas a positional argument could previously freely begin with the dash character, that can now be ambiguous with the --key=value syntax. The solution is to disambiguate by putting such positional values after a --:

/f query --key=value -- --ambiguous-positional-value--

This should be rare enough that the regression is worth the new feature.

This adds a Jinja2 direct dependency to the project. It was already a transitive dependency via altair if mycli was installed with dataframe support.

Fixes #1871

Edit: it looks like there is a bug here in that we determine that a key is unused unless it is referred to explicitly. That logic should apply to completions only. Will fix, but it is a real edge case, and this PR is still reviewable. Fixed.

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the AUTHORS file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

@rolandwalker rolandwalker self-assigned this Aug 3, 2026
@rolandwalker
rolandwalker force-pushed the RW/jinja2-favorite-queries-optional-parameters branch from 949c9be to 7e5d868 Compare August 3, 2026 11:04
@rolandwalker
rolandwalker force-pushed the RW/jinja2-favorite-queries-optional-parameters branch from 7e5d868 to a4acc39 Compare August 3, 2026 18:09
except FavoriteQueryArgumentError as exc:
yield SQLResult(status=f'Invalid favorite query arguments: {exc}')
return
except Exception as exc:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This could be a more specific jinja TemplateError, then a following more generic one to catch everything else

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea.

@scottnemes scottnemes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added one comment, looks good otherwise

allowing optional arguments and control blocks in favorite queries.

Include completions for Jinja2 key names.  Supporting the completions is
quite a bit more code than the core functionality!  Completions also
can only be offered if the key name is referred to explicitly in the
template (not merely iterated over, which is technically possible).

Extensively update the config file and internal helpdoc with examples
and notes.

It seems arbitrary to choose whether Jinja2 templates are processed
before or after the legacy positional arguments are substituted.  I
chose Jinja2 before positional.  The agent hacked together a UUID-based
solution for this, which may not be pretty.

There is one regression: whereas a positional argument could previously
freely begin with the dash character, that can now be ambiguous with the
--key=value syntax.  The solution is to disambiguate by putting such
positional values after a "--":

    /f query --key=value -- --ambiguous-positional-value--

This should be rare enough that the regression is worth the new feature.

This adds a Jinja2 direct dependency to the project.  It was already a
transitive dependency via altair if mycli was installed with dataframe
support.
@rolandwalker
rolandwalker force-pushed the RW/jinja2-favorite-queries-optional-parameters branch from a4acc39 to f3a877a Compare August 4, 2026 08:40
@rolandwalker
rolandwalker merged commit e50e096 into main Aug 4, 2026
11 checks passed
@rolandwalker
rolandwalker deleted the RW/jinja2-favorite-queries-optional-parameters branch August 4, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How about Favorite queries Positional Parameters add Default values support

2 participants