Hi,
I want to visualize data from jupyter to Vega to Kibana.
I got a reference How to bring Jupyter Notebook visualizations to Kibana dashboards for data science | Elastic Blog
But it is not working.
So finally, I found this
But I did not understand in KIBANA visualization api.
I am implementing it into mylocal computer without elasticsearch version 8.4.1 and I disabled login security in elasticsearch.yml.
When I used below code into jupyter to send visualization to kibana.
import eland as ed
import datetime
import altair as alt
import eland as ed
import json
import numpy as np
import matplotlib.pyplot as plt
import vega_datasets
from elasticsearch import Elasticsearch
#cloud_id = "secret"
#http_auth = ("username", "password")
#es = Elasticsearch(cloud_id=cloud_id, http_auth=http_auth)
es=Elasticsearch(["http://localhost:9200"])
data = vega_datasets.data
pd_df = data.cars()
url = 'http://localhost:9200/pandas_to_eland/_search?size=1000'
#print(url)
url_data = alt.Data(url=url, format=alt.DataFormat(property='hits.hits',type='json'))
fields = ed_df.columns
rename_dict = dict((a, 'datum._source.'+a) for a in fields)
chart = alt.Chart(url_data).transform_calculate(**rename_dict).mark_circle(size=8).encode(
alt.X(alt.repeat("column"), type='quantitative'),
alt.Y(alt.repeat("row"), type='quantitative'),
color='Origin:N'
).properties(
width=150,
height=150
).repeat(
row=['Horsepower',],
column=['Miles_per_Gallon']
).interactive()
chart
def saveVegaVis(client, index, visName, altairChart, resultSize=100, timeField=True):
chart_json = json.loads(altairChart.to_json())
visState = {
"type": "vega",
"aggs": [],
"params": {
"spec": json.dumps(chart_json, sort_keys=True, indent=4, separators=(',', ': ')),
},
"title": visName
}
visSavedObject={
#"visualization" : {
"title" : visName,
"visState" : json.dumps(visState, sort_keys=True, indent=4, separators=(',', ': ')),
"uiStateJSON" : "{}",
"description" : "",
"version" : 1,
"kibanaSavedObjectMeta" : {
"searchSourceJSON" : json.dumps({
"query": {
"language": "kuery",
"query": ""
},
"filter": []
}),
}
#},
#"type" : "visualization",
#"references" : [ ],
#"migrationVersion" : {
# "visualization" : "8.0.0"
#},
#"updated_at" : datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")
}
import requests
myurl = 'http://localhost:5601/api/saved_objects/visualization'
headers = {
'kbn-xsrf' : 'true',
'Content-Type' : 'application/json'
}
x = requests.post(myurl, data=json.dumps(visSavedObject), headers=headers)
print(x.text)
#return client.index(index=index,id='visualization:'+visName,body=visSavedObject)
saveVegaVis(es, 'test_visuals', 'def-vega-cars-1', chart, resultSize=1000)
It gives me an error
{"statusCode":400,"error":"Bad Request","message":"[request body.attributes]: expected value of type [object] but got [undefined]"}
I did not understand what is happening.
Kindly help me on this issue.
Thanks in advance,
Vikram Singh