Commit bf224aa1 authored by 20after4's avatar 20after4
Browse files

use json api for accessing data from vega charts

parent 721d44fe
from pprint import pprint
import re
import sqlite3
import sys
from typing import Coroutine, Mapping
import urllib.parse
......@@ -198,8 +199,11 @@ async def render_chart(
await populate_chart_canned_queries(datasette, database, request, chart)
fill_chart_query_options(chart, options_keys)
if chart["library"] == "jinja":
data = await database.execute(chart["query"], query_parameters)
data = [{col: row[col] for col in data.columns} for row in data.rows]
try:
data = await database.execute(chart["query"], query_parameters)
data = [{col: row[col] for col in data.columns} for row in data.rows]
except sqlite3.ProgrammingError:
return
chart["html"] = Markup(
await datasette.render_template(
chart["template"],
......@@ -231,10 +235,12 @@ async def dashboard_chart(request, datasette):
except KeyError:
raise NotFound(f"Chart does not exist: {chart_slug}")
database: Database = None # type: ignore
db = chart.get("db")
if db:
database = datasette.get_database(db)
await check_permission_execute_sql(request, datasette, database)
await populate_dashboard_filter_queries(database, dashboard)
options_keys = get_dashboard_filters_keys(request, dashboard)
query_parameters = get_dashboard_filters(request, options_keys)
......
......@@ -8,14 +8,14 @@ function renderVegaChart(el, chart, query_string, height_style = undefined) {
if (type == 'sql') {
return {
name: dataspec['name'],
url: `/${chart.db}.csv?sql=${query}&${query_string}`,
format: {'type': 'csv'}
url: `/${chart.db}.json?_shape=objects&sql=${query}&${query_string}`,
format: {'type': 'json', 'property': 'rows'}
}
} else if (type == 'query') {
return {
name: dataspec['name'],
url: `/${chart.db}/${query}.csv?${query_string}`,
format: {'type': 'csv'}
url: `/${chart.db}/${query}.json?_shape=objects&${query_string}`,
format: {'type': 'json', "proprty": "rows"}
}
}
}
......@@ -43,8 +43,11 @@ function renderVegaChart(el, chart, query_string, height_style = undefined) {
};
vegaEmbed(el, spec).then(function(result) {
result.view.logLevel(vega.Debug);
console.log('data:',result.view.data('data'));
console.log('Vega-embed view:',result.view);
console.log('data source:',result.view._runtime.data.data.input.value);
console.log('data transformed:',result.view._runtime.data.data);
for (var i in chart.data) {
var dataspec = chart.data[i];
var ds = datasource(chart, dataspec, query_string);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment