Coverage for src/ipyvizzustory/storylib/template.py: 100%
6 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-26 16:47 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-26 16:47 +0000
1"""A module for storing the `HTML` templates."""
3VIZZU_STORY: str = (
4 "https://cdn.jsdelivr.net/npm/vizzu-story@0.8/dist/vizzu-story.min.js"
5)
6"""A variable for storing the default url of the `vizzu-story` package."""
8DISPLAY_INDENT: str = " "
9"""A variable for storing the default indent in the `HTML` template."""
11DISPLAY_TEMPLATE: str = """
12<div>
13 <vizzu-player id="{id}" {vizzu} {start_slide} controller></vizzu-player>
14 <script type="module">
15 import VizzuPlayer from "{vizzu_story}";
17 class IpyvizzuStory {{
18 static version = "{version}";
19 static analytics = undefined;
21 static changeAnalyticsTo(analytics) {{
22 if (IpyvizzuStory.analytics !== analytics) {{
23 console.log("ipyvizzu-story gather usage stats:", analytics);
24 IpyvizzuStory.analytics = analytics;
25 }}
26 if (analytics) {{
27 IpyvizzuStory._addHeadScript();
28 }} else {{
29 IpyvizzuStory._removeScript("ipyvizzu-story-analytics-head");
30 }}
31 }}
33 static _addHeadScript() {{
34 const scriptId = "ipyvizzu-story-analytics-head";
35 if (!IpyvizzuStory._isScriptAppended(scriptId)) {{
36 const script = document.createElement("script");
37 script.defer = true;
38 script.src = "https://plausible.io/js/script.local.js";
39 script.dataset.domain = "usage.ipyvizzu-story.com";
40 script.id = scriptId;
41 document.getElementsByTagName("head")[0].appendChild(script);
42 }}
43 }}
45 static _isScriptAppended(id) {{
46 return document.querySelector(`script[id="${{id}}"]`) !== null;
47 }}
49 static _removeScript(id) {{
50 const script = document.getElementById(id);
51 if (script) script.remove();
52 }}
53 }}
55 if (IpyvizzuStory.version !== window.IpyvizzuStory?.version) {{
56 window.IpyvizzuStory = IpyvizzuStory;
57 console.log(`ipyvizzu-story ${{IpyvizzuStory.version}}`);
58 }}
60 window.IpyvizzuStory?.changeAnalyticsTo({analytics});
62 class Plugins {{
63 static _resolveVizzuVersion(vp) {{
64 const url = vp.vizzuUrl;
65 const versionMatch = url.match(/vizzu@([^\\/]+)\\//);
66 return versionMatch[1];
67 }}
69 static _resolveUrl(plugin, tag) {{
70 if (!plugin.includes('/')) {{
71 const jsdelivr = "https://cdn.jsdelivr.net/npm/@vizzu";
72 return `${{jsdelivr}}/${{plugin}}@${{tag}}/dist/mjs/index.min.js`;
73 }}
74 return plugin;
75 }}
77 static register(vp, chart, plugins) {{
78 const tag = `vizzu-${{Plugins._resolveVizzuVersion(vp)}}`;
79 const pluginsRegistered = [];
80 for (const plugin of plugins) {{
81 const pluginUrl = Plugins._resolveUrl(plugin.plugin, tag);
82 const pluginRegistered = import(pluginUrl).then(pluginModule => {{
83 const pluginInstance = new pluginModule[plugin.name](plugin.options);
84 chart.feature(pluginInstance, true);
85 }}).catch((error) => {{
86 console.error('Error importing plugin:', pluginUrl, error)
87 }});
88 pluginsRegistered.push(pluginRegistered);
89 }}
90 return Promise.all(pluginsRegistered);
91 }}
92 }}
94 const vp = document.getElementById("{id}");
95 vp.initializing.then(chart => {{
96 const lib = vp.Vizzu;
98 // story.set_size()
99 {chart_size}
101 // story.add_plugin()
102 const plugins = [];
103 {chart_plugins}
104 Plugins.register(vp, chart, plugins).then(() => {{
105 // story.set_feature()
106 {chart_features}
107 // story.add_event()
108 {chart_events}
110 const vizzuPlayerData = {vizzu_player_data};
111 vp.slides = vizzuPlayerData;
112 }});
113 }});
114 </script>
115</div>
116"""
117"""A variable for storing the `vizzu-story` `HTML` template."""