1
"""
2
Utilities for building custom models included in panel.
3
"""
4 2
from __future__ import absolute_import, division, unicode_literals
5

6
#---------------------------------------------------------------------
7
# Public API
8
#---------------------------------------------------------------------
9

10 2
def require_components():
11
    """
12
    Returns JS snippet to load the required dependencies in the classic
13
    notebook using REQUIRE JS.
14
    """
15 0
    from .config import config
16

17 0
    configs, requirements, exports = [], [], []
18 0
    js_requires = []
19

20 0
    from bokeh.model import Model
21 0
    for qual_name, model in Model.model_class_reverse_map.items():
22
        # We need to enable Models from Panel as well as Panel extensions
23
        # like awesome_panel_extensions.
24
        # The Bokeh models do not have "." in the qual_name
25 0
        if "." in qual_name:
26 0
            js_requires.append(model)
27

28 0
    for export, js in config.js_files.items():
29 0
        name = js.split('/')[-1].replace('.min', '').split('.')[-2]
30 0
        conf = {'paths': {name: js[:-3]}, 'exports': {name: export}}
31 0
        js_requires.append(conf)
32

33 0
    skip_import = {}
34 0
    for model in js_requires:
35 0
        if hasattr(model, '__js_skip__'):
36 0
            skip_import.update(model.__js_skip__)
37

38 0
        if not (hasattr(model, '__js_require__') or isinstance(model, dict)):
39 0
            continue
40

41 0
        if isinstance(model, dict):
42 0
            model_require = model
43
        else:
44 0
            model_require = model.__js_require__
45

46 0
        model_exports = model_require.pop('exports', {})
47 0
        if not any(model_require == config for config in configs):
48 0
            configs.append(model_require)
49

50 0
        for req in list(model_require.get('paths', [])):
51 0
            if isinstance(req, tuple):
52 0
                model_require['paths'][req[0]] = model_require['paths'].pop(req)
53

54 0
            export = req[0] if isinstance(req, tuple) else req
55 0
            if export not in model_exports:
56 0
                continue
57

58 0
            if isinstance(req, tuple):
59 0
                for r in req[1]:
60 0
                    if r not in requirements:
61 0
                        requirements.append(r)
62 0
                req = req[0]
63 0
            elif req not in requirements:
64 0
                requirements.append(req)
65

66 0
            export = model_exports[req]
67 0
            for e in (export if isinstance(export, list) else [export]):
68 0
                if export not in exports and export is not None:
69 0
                    exports.append(export)
70 0
    return configs, requirements, exports, skip_import

Read our documentation on viewing source code .

Loading