Skip to content

Commit d7979b5

Browse files
authored
Add a button to download bandstructure and DOS Json data (#46) (#47)
* make it possible to download json files from single bandstructure plot * make download jsons for both bandstructure and DOS data * clean example cache * upgrade pre-commit package to fix the fail of the workflow * change back pre-commit version to v2.11.1 * fix the pre-commit error by using isort v5.12.0 * fix the pre-commit error by upgrading pre-commit v3.0.4 * install isort firstly * fix the pre-commit error by changing pre-commit-config file
1 parent 1d58a3b commit d7979b5

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919

2020
- name: Install dependencies
2121
run: |
22-
python -m pip install pre-commit==2.11.1
22+
python -m pip install pre-commit==3.0.4
2323
2424
- name: Run pre-commit
2525
run: pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
exclude: widget_bandsplot/__init__.py
3131

3232
- repo: https://github.com/pycqa/isort
33-
rev: 5.10.1
33+
rev: 5.12.0
3434
hooks:
3535
- id: isort
3636
args: [--profile, black, --filter-files]

examples/bandsplot-example.ipynb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@
188188
"## Plot two bandstructure data"
189189
]
190190
},
191+
{
192+
"cell_type": "raw",
193+
"metadata": {},
194+
"source": []
195+
},
191196
{
192197
"cell_type": "code",
193198
"execution_count": null,
@@ -208,7 +213,7 @@
208213
],
209214
"metadata": {
210215
"kernelspec": {
211-
"display_name": "Python 3",
216+
"display_name": "Python 3 (ipykernel)",
212217
"language": "python",
213218
"name": "python3"
214219
},
@@ -222,7 +227,7 @@
222227
"name": "python",
223228
"nbconvert_exporter": "python",
224229
"pygments_lexer": "ipython3",
225-
"version": "3.9.13"
230+
"version": "3.9.12"
226231
},
227232
"voila": {
228233
"authors": "Dou Du and Giovanni Pizzi"

js/lib/bands.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ function bandPlot(bandDivId, bandPathTextBoxId, dataFilePaths, dosFile, showFerm
139139
a.click();
140140
}
141141
}
142+
143+
var theDownloadJsonButton = document.getElementById(bandDivId + "bt-downloadJson");
144+
theDownloadJsonButton.onclick = function () {
145+
if ($.isEmptyObject(dosFile)) {
146+
var a = document.createElement('a');
147+
if (dataFilePaths.length) {
148+
dataFilePaths.forEach(function (dataFilePath, dataIdx) {
149+
a.href = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(dataFilePath));
150+
a.download = 'bandData' + dataIdx + '.json';
151+
a.click();
152+
})
153+
}
154+
}
155+
}
142156
};
143157

144158
if (!$.isEmptyObject(dosFile)) {
@@ -192,6 +206,28 @@ function bandPlot(bandDivId, bandPathTextBoxId, dataFilePaths, dosFile, showFerm
192206
};
193207
a.click();
194208
}
209+
210+
var theDownloadJsonButton = document.getElementById(bandDivId + "bt-downloadJson");
211+
theDownloadJsonButton.onclick = function () {
212+
var a = document.createElement('a');
213+
214+
if (dataFilePaths.length) {
215+
dataFilePaths.forEach(function (dataFilePath, dataIdx) {
216+
a.href = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(dataFilePath));
217+
a.download = 'bandData' + dataIdx + '.json';
218+
a.click();
219+
220+
})
221+
a.href = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(dosFile));
222+
a.download = 'dosData.json';
223+
a.click();
224+
}
225+
else {
226+
a.href = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(dosFile));
227+
a.download = 'dosData.json';
228+
a.click();
229+
}
230+
}
195231
};
196232

197233
// theBandPlot.myChart.options.pan.enabled = true ;

js/lib/bandsplot.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ var BandsplotView = widgets.DOMWidgetView.extend({
9090
+ '<button type="button" id="' + this.uuidCanvas + 'bt-dragPan" class="button-white"> Drag to pan </button>'
9191
+ '<button type="button" id="' + this.uuidCanvas + 'bt-togglePdos" class="button"> Toggle PDOS </button>'
9292
+ '<button type="button" id="' + this.uuidCanvas + 'bt-downloadFigure" class="button"> Download Figure </button>'
93+
+ '<button type="button" id="' + this.uuidCanvas + 'bt-downloadJson" class="button"> Download Json </button>'
9394
+ '</div>');
9495
};
9596

@@ -100,6 +101,7 @@ var BandsplotView = widgets.DOMWidgetView.extend({
100101
+ '<button type="button" id="' + this.uuidCanvas + 'bt-dragPan" class="button-white"> Drag to pan </button>'
101102
+ '<button type="button" id="' + this.uuidCanvas + 'bt-togglePdos" class="button"> Toggle PDOS </button>'
102103
+ '<button type="button" id="' + this.uuidCanvas + 'bt-downloadFigure" class="button"> Download Figure </button>'
104+
+ '<button type="button" id="' + this.uuidCanvas + 'bt-downloadJson" class="button"> Download Json </button>'
103105
+ '</div>');
104106
};
105107

@@ -111,6 +113,7 @@ var BandsplotView = widgets.DOMWidgetView.extend({
111113
+ '<button type="button" id="' + this.uuidCanvas + 'bt-dragZoom" class="button"> Drag (or pinch) to zoom </button>'
112114
+ '<button type="button" id="' + this.uuidCanvas + 'bt-dragPan" class="button-white"> Drag to pan </button>'
113115
+ '<button type="button" id="' + this.uuidCanvas + 'bt-downloadFigure" class="button"> Download Figure </button>'
116+
+ '<button type="button" id="' + this.uuidCanvas + 'bt-downloadJson" class="button"> Download Json </button>'
114117
+ '</div>');
115118
};
116119
},

0 commit comments

Comments
 (0)