-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Jinja index rendering #281
Comments
I'm having a bit of trouble understanding the "pros" here... Is it mostly that it would be easier for people who already use Jinja elsewhere to reuse bits of those apps? Because this is just rendered one time, without any conditional variables being injected etc (other than JS and CSS lists).. |
I don't really see the pros here also. The big downside that is not mentioned is that this introduces a functional dependency on Jinja2. Even though Jinja2 is bundled with Flask, it's not used by default. In addition to introducing this dependency, we'd now also require people to learn yet another markup language, as opposed to vanilla HTML. Currently, there's nothing stopping someone subclassing the |
I would also argue that, if this is our proposed solution in the docs, could create confusion about the use of |
(perhaps this isn't really a distinct issue from #265... I'm having trouble picking apart which bits of my feedback go in which of the two issues :) I also don't like the The biggest problem that I see here is that this proposal seems to reduce the difficulty of very-advanced use cases at the expense of raising the complexity of very-basic use cases, by forcing people to learn Jinja as opposed to the string interpolation suggested in #265. This isn't a deal breaker for me but it's something to think about. |
(sorry, I definitely didn't mean to close the issue, I mis-clicked!) |
The |
Thanks to everyone who is providing feedback on this one :) I think that I'm still in favor of the interpolated string option:
For me, I think the main reason that I prefer the interpolated string method is that I find it to be more transparent in three areas:
Here are some use cases that I have in mind:
In those cases, if I used And while I like how simple something like this looks:
I feel like in order to understand that, I'd need knowledge of:
Since our index string is so simple and readable, I feel like it's just as much work to read the index string and see exactly where the different blocks go. In response to some other design decisions:
|
For what it's worth, here's what I did to build a Dash template from an existing Flask, Jinja template. In case you're wondering, I have a hybrid web app that uses both Flask and Dash. First extend your "base.html" template into a Dash-specific template (e.g. "base_dash.html"), using HTML comments for the things Dash needs to replace (e.g. {% extends "base.html" %}
{% block head %}
<!-- metas -->
<title>
<!-- title -->
</title>
<!-- favicon -->
<!-- css -->
{% endblock %}
{% block body %}
<!-- app_entry -->
<footer>
<!-- config -->
<!-- scripts -->
<!-- renderer -->
</footer>
{% endblock %} Back in Python, when setting up the Dash app: dashapp = dash.Dash()
# FYI, you need both an app context and a request context to use url_for() in the Jinja2 templates
with app.app_context(), app.test_request_context():
layout_dash = pathlib.Path(get_root_path(__name__)).joinpath("templates").joinpath("base_dash.html")
with open(layout_dash, "r") as f:
html_body = render_template_string(f.read())
comments_to_replace = ("metas", "title", "favicon", "css", "app_entry", "config", "scripts", "renderer")
for comment in comments_to_replace:
html_body = html_body.replace(f"<!-- {comment} -->", "{%" + comment + "%}")
dashapp.index_string = html_body |
Right now the rendering of the dash index is done with a simple string formatting.
This is not ideal, flask is shipped with jinja2 and we should use it to render the
dash index page instead.
Pros:
Cons:
Here's the template I've come up with on branch
jinja-index-render
:This template can be overridden by the user by changing the config of dash.
app = dash.Dash(index_template='home.html')
The user can then modify blocks as he wishes:
I'd like feedback on this, this is a step I want to take before adding new features such as #265 and #66.
The text was updated successfully, but these errors were encountered: