andreasgerstmayr/fava-dashboards

Using fava templates?

IguanaBen opened this issue · 6 comments

I really like this dashboard plugin. I was wondering if there's a way to use some of fava's included templates as an output with fava_templates? What I'd like to do is run a bql query and have it output a 'pretty' table like is does when you run a custom query in fava. Your setup it so flexible, I can run a bpl query and iterate over the results and generate the same html, but I'm wondering if there's a cleaner / simpler way to do this by hooking into the _query_table.html template in fava instead of effectively recreating it in each script.

Thanks!

Would using YAML anchors work? Something like the following (note the *table reference in the second panel):

- title: Last 5 expense postings
    queries:
    - bql: SELECT date, payee, narration, position WHERE account ~ "^Expenses:" ORDER BY date DESC LIMIT 5
    type: html
    script: &table |
      const rows = panel.queries[0].result.map(row => `
        <tr>
          <td>${row.date}</td>
          <td>${row.payee}</td>
          <td>${row.narration}</td>
          <td class="num"><span class="num">${row.position.units.number} ${row.position.units.currency}</span></td>
        </tr>
      `);

      return `
        <table is="sortable-table" class="queryresults">
          <thead>
            <tr>
              <th data-sort="string">date</th>
              <th data-sort="string">payee</th>
              <th data-sort="string">narration</th>
              <th data-sort="num">position</th>
            </tr>
          </thead>
          <tbody>
            ${rows.join('\n')}
          </tbody>
        </table>
      `;

  - title: Last 5 income postings
    queries:
    - bql: SELECT date, payee, narration, position WHERE account ~ "^Income:" ORDER BY date DESC LIMIT 5
    type: html
    script: *table

I guess it'd be technically possible to hook directly into Fava's templates, but I'm not sure how stable they are (i.e. any change there in a future version will break the dashboard).

Oh that's cool. I didn't appreciate YAML anchors. That makes it much better for writing a custom query and getting to reuse it. What's neat about the query_table used in fava is it generates the table headers automatically from query, so you get something well-formatted just from the bql alone. I'll take a look and see if the query returns this info so that functionality can be re-created. If so, it might be a useful / general purpose script from bql->table and maybe it could be added fava-dashboard so it can be accessed in the yaml file without writing out that script.

What's neat about the query_table used in fava is it generates the table headers automatically from query, so you get something well-formatted just from the bql alone.

Hm, yeah that's quite cool indeed.
I created a draft PR to allow templates in #6, can you try it out and see if that works for your use case?

Following! I'm starting using Fava and having a dashboard with tables and such would be useful. Thanks!

That's perfect!! I just tried out the support-jinja2-templates branch and its really easy to use and super-powerful and let's me do exactly what I wanted. Thank you so much!

Awesome, thanks for reporting back!