Repository navigation
Add dask.widgets and migrate HTML reprs to jinja2 - #8019
Conversation
|
Just a note that this will be very good content to include in the dask developer guide, once we have the organization sorted :) |
jsignell
left a comment
There was a problem hiding this comment.
This all looks great, but I don't think jinja2 should be a required dependency of dask (similar to how ipywidgets isn't a dependency). All of these display methods are only used in notebooks and notebooks already depend on jinja2, so I think it's just a matter of adding a skip if ImportError to the widgets module.
Absolutely! I thought about adding a docs page now, but wasn't sure where to put it. So decided to put it in the PR description for now.
I can see this being applicable in more places than notebooks. The dashboard in distributed and performance reports are prime candidates. Maybe it should be added to the My brain isn't working and I'm struggling to work out how to do the skip |
Oh! I missed that this is also affecting the dashboard. In that case, yeah I think it belongs wherever bokeh is.
dataframe is a good example with a clear error message: https://github.com/dask/dask/blob/main/dask/dataframe/__init__.py |
|
Awesome thanks @jsignell. I've updated that. Not 100% sure if I've done the right thing in the CI. |
|
Hrm no that's not going to work, anywhere we import the widgets utils (like array or distributed) will now raise that import error. Do you think I should move the |
Yeah that's one approach, or the widgets module could not trigger the warning and instead the warning could be triggered from within the Like you could move the try/except ImportError to the try:
from .widgets import FILTERS, TEMPLATE_PATHS, get_environment, get_template
except ImportError as e:
msg = (
"Dask diagnostics requirements are not installed.\n\n"
"Please either conda or pip install as follows:\n\n"
" conda install dask # either conda install\n"
' python -m pip install "dask[diagnostics]" --upgrade # or python -m pip install'
)
FILTERS = dict()
TEMPLATE_PATHS = []
def get_environment(*args, **kwargs):
raise ImportError(msg) from e
def get_template(*args, **kwargs):
raise ImportError(msg) from e |
|
Oooh I like that! |
|
This looks good to me! I'll let @jrbourbeau give a final 👍 |
| def typename(typ, short=False): | ||
| """Return name of type | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from distributed import Scheduler | ||
| >>> typename(Scheduler) | ||
| 'distributed.scheduler.Scheduler' | ||
| >>> typename(Scheduler, short=True) | ||
| 'distributed.Scheduler' | ||
| """ | ||
| try: | ||
| if short: | ||
| module, *_ = typ.__module__.split(".") | ||
| else: | ||
| module = typ.__module__ | ||
| return module + "." + typ.__name__ | ||
| except AttributeError: | ||
| return str(typ) |
There was a problem hiding this comment.
There's already a typename function in this module. Can we use that instead? It looks like we might be able to use the other typename as-is. It doesn't have the short= keyword, but IIUC we're not using that anywhere anyways
There was a problem hiding this comment.
Oh interesting I hadn't spotted that! I just upstreamed the one from distributed. I've merged them now.
I added short= myself because I found a few places where this functionality was duplicated and used the short version. So this helps reduce some code duplication in other places.
| " conda install dask # either conda install\n" | ||
| ' python -m pip install "dask[diagnostics]" --upgrade # or python -m pip install' | ||
| ) | ||
| exception = e |
There was a problem hiding this comment.
Nitpick: I'm not sure why this is needed. Is except ImportError as exception: a few lines above equivalent?
There was a problem hiding this comment.
It seems that the e gets dereferenced outside the try block despite it being used within the method.
MCRE
try:
raise Exception()
except Exception as e:
def foo():
raise Exception() from e
foo()Traceback (most recent call last):
File "/tmp/test.py", line 7, in <module>
foo()
File "/tmp/test.py", line 5, in foo
raise Exception() from e
NameError: name 'e' is not definedSo you need to assign it to a variable explicitly to maintain the reference.
try:
raise Exception()
except Exception as e:
exception = e # Make a reference here
def foo():
raise Exception() from exception
foo()Traceback (most recent call last):
File "/tmp/test.py", line 2, in <module>
raise Exception()
Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/tmp/test.py", line 8, in <module>
foo()
File "/tmp/test.py", line 6, in foo
raise Exception() from exception
Exception|
I am planning on merging this in 24 hours if there is no further feedback. |
|
Merging |
This PR extends dask/distributed#5188. This goal of this PR is to make HTML reprs more maintainable by using jinja2 templates instead of storing HTML in multiline strings. It also aims to make it easy for downstream projects to make use of and extend this tooling.
Changes
distributed.widgets(I will PR to distributed separately to use this instead).jinja2a required dependency.TEMPLATE_PATHSandFILTERSconstants which other packages can make use of.dask.widgets.Array,Dataframe,HighLevelGraphandLayerto use widget templates.distributed.utils.typenametodask.utils.typenameas it was useful in those migrations.Writing HTML reprs
To add a new HTML repr you can import
dask.widgets.get_templatewhich can be used to load jinja2 templates fromdask/widgets/templates.Jinja2 also supports filters which are functions that can be used via a
|pipe within the template. Thedask.widgetsmodule adds a bunch of utilities fromdask.utilsto the default filters.For downstream projects
Downstream projects can register their own templates directories and filters, similar to how they can extend the Dask config system.
Example
Within that project they can use the widgets tooling to access their own templates and use their own filters.
Then in
my_awesome_template.html.j2in their custom templates directory.The
MANIFEST.infile probably will need updating in the subproject to ensure that*.j2files are packaged.