2021-02-20 13:39:54 +00:00
|
|
|
from django import template
|
|
|
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
2021-02-21 19:13:22 +00:00
|
|
|
import holoviews as hv
|
|
|
|
|
2021-02-22 19:10:37 +00:00
|
|
|
from bokeh.embed import file_html
|
|
|
|
from bokeh.resources import INLINE
|
|
|
|
from bokeh.models.tools import PanTool, WheelZoomTool
|
|
|
|
|
2021-02-20 13:39:54 +00:00
|
|
|
import base64
|
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
|
|
|
|
@register.simple_tag
|
|
|
|
def pildata(image):
|
|
|
|
data = BytesIO()
|
|
|
|
image.save(data, "JPEG")
|
|
|
|
content = base64.b64encode(data.getvalue()).decode("UTF-8")
|
2021-02-21 19:13:22 +00:00
|
|
|
return f"data:img/jpeg;base64,{content}"
|
|
|
|
|
|
|
|
@register.simple_tag
|
2021-03-01 17:05:14 +00:00
|
|
|
def bkhtml(bkobject, lock_y=False):
|
|
|
|
if lock_y:
|
|
|
|
pan_tool = bkobject.select(dict(type=PanTool))
|
|
|
|
pan_tool.dimensions = "width"
|
2021-02-22 19:10:37 +00:00
|
|
|
|
2021-03-01 17:05:14 +00:00
|
|
|
zoom_tool = bkobject.select(dict(type=WheelZoomTool))
|
|
|
|
zoom_tool.dimensions = "width"
|
2021-02-22 19:10:37 +00:00
|
|
|
|
2021-03-01 17:05:14 +00:00
|
|
|
html = file_html(bkobject, INLINE)
|
2021-02-21 19:13:22 +00:00
|
|
|
html = html.replace("http://localhost:5006/static/extensions/panel/css", "/static/frontend/vendor/panel")
|
|
|
|
|
2021-03-01 17:05:14 +00:00
|
|
|
return html
|
|
|
|
|
|
|
|
@register.simple_tag
|
|
|
|
def hvhtml(hvobject, lock_y=True):
|
|
|
|
return bkhtml(hv.render(hvobject), lock_y)
|
2021-02-21 19:13:22 +00:00
|
|
|
|
|
|
|
@register.simple_tag
|
|
|
|
def hvdata(hvobject):
|
|
|
|
html = hvhtml(hvobject)
|
|
|
|
return f"data:text/html;charset=utf-8,{html}"
|