superset icon indicating copy to clipboard operation
superset copied to clipboard

[Design]Jinja2 support in Markup Viz

Open kartikshanbhag3333 opened this issue 7 years ago • 5 comments

Make sure these boxes are checked before submitting your issue - thank you!

  • [x] I have checked the superset logs for python stacktraces and included it here as text if there are any.
  • [x] I have reproduced the issue with at least the latest released version of superset.
  • [x] I have checked the issue tracker for the same issue and I haven't found one similar.

Superset version

0.28.1

Expected results

Jinja2 Templating in Markup Viz

Actual results

image

Steps to reproduce

def query_obj(self):
    return None

def get_df(self, query_obj=None):
    return None

def get_data(self, df):
    markup_type = self.form_data.get('markup_type')
    code = self.form_data.get('code', '')
    if markup_type == 'markdown':
        code = markdown(code)
        return dict(html=code, theme_css=get_css_manifest_files('theme'))
    else:
        print(template_processor.context["current_username"]())
        return dict(
            html=jinja2.Environment().from_string(code).render(template_processor=template_processor),
            theme_css=get_css_manifest_files('theme')
        )`

The question here is, how safe is this? template_processor is object of jinja_context.BaseTemplateProcessor

kartikshanbhag3333 avatar Jan 28 '19 17:01 kartikshanbhag3333

Markup viz is getting deprecated though, and you probably want jinja2.sandbox.SandboxedEnvironment

mistercrunch avatar Jan 29 '19 06:01 mistercrunch

Hi @mistercrunch this viz will be quite good for our project. It will give us lot of possibilities to create a rich Welcome dashboards for the user. It will be great if we can use Jinja2 templates and query a datasource.

Using Jinja2 templates: image

In my case I'm using existing BaseTemplateProcessor

from superset.jinja_context import get_template_processor

class MarkupViz(BaseViz):

    """Use html or markdown to create a free form widget"""

    viz_type = "markup"
    verbose_name = _("Markup")
    is_timeseries = False

    def query_obj(self):
        return None

    def get_df(self, query_obj=None):
        return None

    def get_code(self):
        code = self.form_data.get("code", "")
        processor = get_template_processor(self.datasource.database)
        return processor.process_template(code)

    def get_data(self, df):
        markup_type = self.form_data.get("markup_type")
        code = self.get_code()
        if markup_type == "markdown":
            code = markdown(code, extensions=['tables'])
        return dict(html=code, theme_css=get_css_manifest_files("theme"))

Let me know if this is something good/NTH in order to create a PR.

gbrian avatar Jul 15 '19 10:07 gbrian

Added access to data source data by just copying from TableViz implementation. Dataset can be accessed as an array through data variable

image

gbrian avatar Jul 15 '19 10:07 gbrian

Is this just a #wont-fix because the markup viz is getting deprecated, jinja2 templating is unsafe or getting deprecated, or both? What about a slice that allows for templated html/markdown with just the query results?

micimize avatar Sep 07 '19 21:09 micimize

Thanks to your advices I was able to enable templating in markup chart. Thanks!

zufolo441 avatar May 28 '20 12:05 zufolo441