Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Design]Jinja2 support in Markup Viz #6771

Closed
3 tasks done
kartikshanbhag3333 opened this issue Jan 28, 2019 · 5 comments
Closed
3 tasks done

[Design]Jinja2 support in Markup Viz #6771

kartikshanbhag3333 opened this issue Jan 28, 2019 · 5 comments
Labels
enhancement:request Enhancement request submitted by anyone from the community

Comments

@kartikshanbhag3333
Copy link

kartikshanbhag3333 commented Jan 28, 2019

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

  • I have checked the superset logs for python stacktraces and included it here as text if there are any.
  • I have reproduced the issue with at least the latest released version of superset.
  • 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

@mistercrunch
Copy link
Member

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

@kristw kristw added enhancement:request Enhancement request submitted by anyone from the community wont-fix labels Feb 7, 2019
@kristw kristw closed this as completed Feb 7, 2019
@gbrian
Copy link
Contributor

gbrian commented Jul 15, 2019

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
Copy link
Contributor

gbrian commented Jul 15, 2019

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

image

@micimize
Copy link
Contributor

micimize commented Sep 7, 2019

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?

@zufolo441
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement:request Enhancement request submitted by anyone from the community
Projects
None yet
Development

No branches or pull requests

6 participants