MarcSkovMadsen commited on
Commit
5dd4d14
β€’
1 Parent(s): aaf3d70

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +17 -5
  2. index.html +0 -0
  3. index.js +146 -0
  4. index.py +41 -0
  5. requirements.txt +2 -0
README.md CHANGED
@@ -1,11 +1,23 @@
1
  ---
2
- title: Table Of Elements With Wikipedia Row Content
3
- emoji: πŸŒ–
4
- colorFrom: gray
5
- colorTo: indigo
6
  sdk: static
7
  pinned: false
8
  license: mit
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Table of elements with Wikipedia row content
3
+ emoji: πŸ‘¨β€πŸ”¬
4
+ colorFrom: yellow
5
+ colorTo: red
6
  sdk: static
7
  pinned: false
8
  license: mit
9
  ---
10
 
11
+ See [table_of_elements_with_wikipedia_row_content](https://awesome-panel.org/resources/table_of_elements_with_wikipedia_row_content/) by [awesome-panel.org](https://awesome-panel.org) for more info.
12
+
13
+ ## Serve
14
+
15
+ ```python
16
+ panel serve *.py --autoreload
17
+ ```
18
+
19
+ ## Build
20
+
21
+ ```bash
22
+ panel convert *.py --to pyodide-worker
23
+ ```
index.html CHANGED
The diff for this file is too large to render. See raw diff
 
index.js ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ importScripts("https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.js");
2
+
3
+ function sendPatch(patch, buffers, msg_id) {
4
+ self.postMessage({
5
+ type: 'patch',
6
+ patch: patch,
7
+ buffers: buffers
8
+ })
9
+ }
10
+
11
+ async function startApplication() {
12
+ console.log("Loading pyodide!");
13
+ self.postMessage({type: 'status', msg: 'Loading pyodide'})
14
+ self.pyodide = await loadPyodide();
15
+ self.pyodide.globals.set("sendPatch", sendPatch);
16
+ console.log("Loaded!");
17
+ await self.pyodide.loadPackage("micropip");
18
+ const env_spec = ['https://cdn.holoviz.org/panel/wheels/bokeh-3.3.2-py3-none-any.whl', 'https://cdn.holoviz.org/panel/1.3.6/dist/wheels/panel-1.3.6-py3-none-any.whl', 'pyodide-http==0.2.1', 'pandas']
19
+ for (const pkg of env_spec) {
20
+ let pkg_name;
21
+ if (pkg.endsWith('.whl')) {
22
+ pkg_name = pkg.split('/').slice(-1)[0].split('-')[0]
23
+ } else {
24
+ pkg_name = pkg
25
+ }
26
+ self.postMessage({type: 'status', msg: `Installing ${pkg_name}`})
27
+ try {
28
+ await self.pyodide.runPythonAsync(`
29
+ import micropip
30
+ await micropip.install('${pkg}');
31
+ `);
32
+ } catch(e) {
33
+ console.log(e)
34
+ self.postMessage({
35
+ type: 'status',
36
+ msg: `Error while installing ${pkg_name}`
37
+ });
38
+ }
39
+ }
40
+ console.log("Packages loaded!");
41
+ self.postMessage({type: 'status', msg: 'Executing code'})
42
+ const code = `
43
+
44
+ import asyncio
45
+
46
+ from panel.io.pyodide import init_doc, write_doc
47
+
48
+ init_doc()
49
+
50
+ """
51
+ Source: https://awesome-panel.org/resources/table_of_elements_with_wikipedia_row_content/
52
+ """
53
+
54
+ import panel as pn
55
+ from bokeh.sampledata.periodic_table import elements
56
+
57
+ pn.extension("tabulator")
58
+
59
+ @pn.cache
60
+ def get_elements():
61
+ return elements[
62
+ ["atomic number", "name", "atomic mass", "metal", "year discovered"]
63
+ ].set_index("atomic number")
64
+
65
+ periodic_df = get_elements()
66
+
67
+ @pn.cache # Caching is a hack to avoid flickering. It seems like row content is loaded twice otherwise
68
+ def content_fn(row):
69
+ return pn.pane.HTML(
70
+ f'<iframe src="https://en.wikipedia.org/wiki/{row["name"]}" width="100%" height="500px"></iframe>',
71
+ sizing_mode="stretch_width"
72
+ )
73
+
74
+ periodic_table = pn.widgets.Tabulator(
75
+ periodic_df,
76
+ layout="fit_columns",
77
+ sizing_mode="stretch_both",
78
+ row_content=content_fn,
79
+ embed_content=True,
80
+ )
81
+
82
+ pn.template.FastListTemplate(
83
+ site="Awesome Panel",
84
+ site_url="https://awesome-panel.org",
85
+ title="Table of Elements with Wikipedia row content",
86
+ main=[periodic_table],
87
+ accent="#F08080",
88
+ main_layout=None,
89
+ main_max_width="1024px",
90
+ ).servable()
91
+
92
+
93
+ await write_doc()
94
+ `
95
+
96
+ try {
97
+ const [docs_json, render_items, root_ids] = await self.pyodide.runPythonAsync(code)
98
+ self.postMessage({
99
+ type: 'render',
100
+ docs_json: docs_json,
101
+ render_items: render_items,
102
+ root_ids: root_ids
103
+ })
104
+ } catch(e) {
105
+ const traceback = `${e}`
106
+ const tblines = traceback.split('\n')
107
+ self.postMessage({
108
+ type: 'status',
109
+ msg: tblines[tblines.length-2]
110
+ });
111
+ throw e
112
+ }
113
+ }
114
+
115
+ self.onmessage = async (event) => {
116
+ const msg = event.data
117
+ if (msg.type === 'rendered') {
118
+ self.pyodide.runPythonAsync(`
119
+ from panel.io.state import state
120
+ from panel.io.pyodide import _link_docs_worker
121
+
122
+ _link_docs_worker(state.curdoc, sendPatch, setter='js')
123
+ `)
124
+ } else if (msg.type === 'patch') {
125
+ self.pyodide.globals.set('patch', msg.patch)
126
+ self.pyodide.runPythonAsync(`
127
+ state.curdoc.apply_json_patch(patch.to_py(), setter='js')
128
+ `)
129
+ self.postMessage({type: 'idle'})
130
+ } else if (msg.type === 'location') {
131
+ self.pyodide.globals.set('location', msg.location)
132
+ self.pyodide.runPythonAsync(`
133
+ import json
134
+ from panel.io.state import state
135
+ from panel.util import edit_readonly
136
+ if state.location:
137
+ loc_data = json.loads(location)
138
+ with edit_readonly(state.location):
139
+ state.location.param.update({
140
+ k: v for k, v in loc_data.items() if k in state.location.param
141
+ })
142
+ `)
143
+ }
144
+ }
145
+
146
+ startApplication()
index.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Source: https://awesome-panel.org/resources/table_of_elements_with_wikipedia_row_content/
3
+ """
4
+
5
+ import panel as pn
6
+ from bokeh.sampledata.periodic_table import elements
7
+
8
+ pn.extension("tabulator")
9
+
10
+ @pn.cache
11
+ def get_elements():
12
+ return elements[
13
+ ["atomic number", "name", "atomic mass", "metal", "year discovered"]
14
+ ].set_index("atomic number")
15
+
16
+ periodic_df = get_elements()
17
+
18
+ @pn.cache # Caching is a hack to avoid flickering. It seems like row content is loaded twice otherwise
19
+ def content_fn(row):
20
+ return pn.pane.HTML(
21
+ f'<iframe src="https://en.wikipedia.org/wiki/{row["name"]}" width="100%" height="500px"></iframe>',
22
+ sizing_mode="stretch_width"
23
+ )
24
+
25
+ periodic_table = pn.widgets.Tabulator(
26
+ periodic_df,
27
+ layout="fit_columns",
28
+ sizing_mode="stretch_both",
29
+ row_content=content_fn,
30
+ embed_content=True,
31
+ )
32
+
33
+ pn.template.FastListTemplate(
34
+ site="Awesome Panel",
35
+ site_url="https://awesome-panel.org",
36
+ title="Table of Elements with Wikipedia row content",
37
+ main=[periodic_table],
38
+ accent="#F08080",
39
+ main_layout=None,
40
+ main_max_width="1024px",
41
+ ).servable()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pandas
2
+ panel