Skip to content

Commit 5495848

Browse files
authored
Merge pull request #10 from openmc-data-storage/adding-download
Adding download
2 parents 4f2be99 + ba1d098 commit 5495848

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ yew-custom-components = { version = "0.2.2", features = ["table", "pagination"]
2424
serde_json = "1.0.138"
2525
yew-hooks = "0.3.3"
2626
yew-router = "0.18"
27-
web-sys = { version = "0.3.77", features = ["HtmlInputElement","Document", "Window"] }
27+
web-sys = { version = "0.3.77", features = ["HtmlInputElement","Document", "Window", "Blob", "Url", "HtmlElement"] }
2828
reqwest = { version = "0.12.12", features = ["json"] }
2929
cached = { version = "0.54.0", features = ["async"] }
30+
wasm-bindgen = "0.2"
31+
wasm-bindgen-futures = "0.4"
32+
js-sys = "0.3"
33+
3034
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
31-
tokio = { version = "1.43.0", features = ["full"] }
35+
tokio = { version = "1.43.0", features = ["full"] }

src/sauce/home.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ use plotly::layout::{AxisType};
1414
use yew::prelude::*;
1515
use serde::Deserialize;
1616

17+
use wasm_bindgen_futures::spawn_local;
18+
use web_sys::HtmlElement;
19+
use web_sys::{Blob, BlobPropertyBag, Url};
20+
use wasm_bindgen::JsValue;
21+
use js_sys::Array;
22+
use web_sys::wasm_bindgen::JsCast;
23+
use serde_json::Value;
24+
25+
1726
#[derive(Debug, Serialize, Deserialize)]
1827
struct ReactionData {
1928
#[serde(rename = "energy")]
@@ -181,6 +190,52 @@ fn convert_string(entry: &Entry) -> String {
181190
output
182191
}
183192

193+
async fn download_xs_cache(selected_indexes: HashSet<usize>) {
194+
let cache = generate_cache(&selected_indexes).await;
195+
196+
// Convert the cache data to a pretty-printed JSON string
197+
let json_data = serde_json::to_string_pretty(&cache).unwrap();
198+
199+
// Deserialize the JSON string into a serde_json::Value
200+
let mut json_value: Value = serde_json::from_str(&json_data).unwrap();
201+
202+
// Remove the "checkbox_selected" key
203+
if let Value::Object(ref mut map) = json_value {
204+
map.remove("checkbox_selected");
205+
}
206+
207+
// Serialize the modified JSON value back to a string
208+
let modified_json_data = serde_json::to_string_pretty(&json_value).unwrap();
209+
210+
211+
// Create a Blob from the JSON data
212+
let blob_options = BlobPropertyBag::new();
213+
blob_options.set_type("application/json");
214+
215+
let blob = Blob::new_with_str_sequence_and_options(
216+
&Array::of1(&JsValue::from_str(&modified_json_data)),
217+
&blob_options,
218+
).unwrap();
219+
220+
// Create a URL for the Blob
221+
let url = Url::create_object_url_with_blob(&blob).unwrap();
222+
223+
// Create a hidden anchor element to trigger the download
224+
let document = web_sys::window().unwrap().document().unwrap();
225+
let a = document.create_element("a").unwrap();
226+
a.set_attribute("href", &url).unwrap();
227+
a.set_attribute("download", "cross_sections_from_xsplot.json").unwrap();
228+
a.set_attribute("style", "display: none;").unwrap();
229+
document.body().unwrap().append_child(&a).unwrap();
230+
231+
// Trigger the download
232+
let a: HtmlElement = a.dyn_into::<HtmlElement>().unwrap();
233+
a.click();
234+
235+
// Clean up by revoking the object URL
236+
Url::revoke_object_url(&url).unwrap();
237+
document.body().unwrap().remove_child(&a).unwrap();
238+
}
184239

185240
#[function_component(Home)]
186241
pub fn home() -> Html {
@@ -381,6 +436,17 @@ pub fn home() -> Html {
381436
})
382437
};
383438

439+
440+
let onclick_download = {
441+
let selected_indexes = selected_indexes.clone();
442+
Callback::from(move |_| {
443+
let selected_indexes = selected_indexes.current().clone();
444+
spawn_local(async move {
445+
download_xs_cache(selected_indexes).await;
446+
});
447+
})
448+
};
449+
384450
// let pagination_options = yew_custom_components::pagination::Options::default()
385451
// .show_prev_next(true)
386452
// .show_first_last(true)
@@ -491,6 +557,15 @@ pub fn home() -> Html {
491557
>
492558
{if *is_y_log { "Switch Y to Linear Scale" } else { "Switch Y to Log Scale" }}
493559
</button>
560+
561+
<button
562+
class="btn btn-primary me-2"
563+
onclick={onclick_download}
564+
>
565+
<i class="fas fa-download me-2"></i>
566+
{" Download Cross Section Data"}
567+
</button>
568+
494569
</div>
495570

496571
<div class="d-flex mb-2">

0 commit comments

Comments
 (0)