From d673bcf626893bbdf573de3dcb0e5df4bd7a35bb Mon Sep 17 00:00:00 2001 From: William-Hill Date: Wed, 7 Nov 2018 10:16:25 -0800 Subject: [PATCH] Import export colormaps (#329) * added exporting colormaps from vcs backend * export json format now matches backend * attempting to get import working * add default param to saveColormap(); pass current_colormap_name as prop to ImportExportModal * convert vcs-style colormap json object to old frontend style for importing; Create a new colormap in VCS before saving when importing a colormap from json * removed unneeded backend code added during import/export development * removed outdated "data" key from state object * extracted convertToOldJson() into separate function --- backend/vcdat/app.py | 16 ++--- .../modals/ColormapEditor/ColormapEditor.jsx | 57 ++++++++-------- .../ColormapEditor/ImportExportModal.jsx | 66 +++++++++++++++---- 3 files changed, 93 insertions(+), 46 deletions(-) diff --git a/backend/vcdat/app.py b/backend/vcdat/app.py index 496c64c..4620166 100644 --- a/backend/vcdat/app.py +++ b/backend/vcdat/app.py @@ -20,7 +20,7 @@ def wrap(*args, **kwargs): return r return wrap - + app = Flask(__name__, static_url_path='') _ = vcs.init() @@ -77,12 +77,12 @@ def serve_resource_file(path): mimetype = "image/svg" else: mimetype = "text/plain" - try: + try: return Response(pkg_resources.resource_string(__name__, "resources/" + path), mimetype=mimetype) except IOError: return Response(status=404) - + @app.route("/plotTemplate") def plot_template(): tmpl = json.loads(request.args["tmpl"]) @@ -108,26 +108,26 @@ def plot_template(): # For certain templates the renWin can be None if(canvas.backend.renWin): # Only call render if renWin exists - canvas.backend.renWin.Render() + canvas.backend.renWin.Render() canvas.png(tmp) # create response from the tmp file, blank or otherwise - resp = send_file(tmp) + resp = send_file(tmp) # Clean up file automatically after request wr = weakref.ref(resp, lambda x: os.remove(tmp)) canvas.close() # clean up temporary boxfill and template we created - del vcs.elements["boxfill"][g.name] + del vcs.elements["boxfill"][g.name] del vcs.elements["template"][t.name] return resp - + @app.route("/getDefaultMethods") @jsonresp def get_default_methods(): default_gms = get_default_gms() return json.dumps(default_gms) - + @app.route("/getColormaps") @jsonresp def get_colormaps(): diff --git a/frontend/src/js/components/modals/ColormapEditor/ColormapEditor.jsx b/frontend/src/js/components/modals/ColormapEditor/ColormapEditor.jsx index 9350301..f6371c8 100644 --- a/frontend/src/js/components/modals/ColormapEditor/ColormapEditor.jsx +++ b/frontend/src/js/components/modals/ColormapEditor/ColormapEditor.jsx @@ -25,7 +25,7 @@ class ColormapEditor extends Component { selected_cells_end: -1, current_colormap: this.props.colormaps[this.props.default_colormap].map(function(arr) { return arr.slice() - }), // an array of arrays representing the current cells + }), // an array of arrays representing the current cells } } @@ -44,7 +44,7 @@ class ColormapEditor extends Component { applyColormap: PropTypes.func, graphics_methods: PropTypes.object, startTour: PropTypes.func, - }; + }; } handleChange(color) { @@ -141,7 +141,7 @@ class ColormapEditor extends Component { selected_cells_start: -1, selected_cells_end: -1, }) - } + } else{ return } @@ -151,12 +151,12 @@ class ColormapEditor extends Component { } } - createNewColormap(new_cm_name){ + createNewColormap(new_cm_name, colormap=this.state.selected_colormap_name){ if(Object.keys(this.props.colormaps).indexOf(new_cm_name) >= 0){ toast.warn("A colormap with that name already exists. Please select a different name", {position: toast.POSITION.BOTTOM_CENTER}) } else{ - return this.createNewColormapInVcs(this.state.selected_colormap_name, new_cm_name).then((result)=>{ + return this.createNewColormapInVcs(colormap, new_cm_name).then((result)=>{ if(result){ this.handleSelectColormap(new_cm_name) this.setState({ @@ -169,11 +169,14 @@ class ColormapEditor extends Component { } } - saveColormap(name){ + saveColormap(name, colormap=this.state.current_colormap){ if(name){ try{ - return vcs.setcolormap(name, this.state.current_colormap).then(() => { - this.props.saveColormap(name, this.state.current_colormap) + return vcs.setcolormap(name, colormap).then(() => { + if (colormap != this.state.current_colormap){ + this.setState({current_colormap: colormap}) + } + this.props.saveColormap(name, colormap) toast.success("Save Successful", { position: toast.POSITION.BOTTOM_CENTER }); PubSub.publish(PubSubEvents.colormap_update, name) }, @@ -183,7 +186,7 @@ class ColormapEditor extends Component { toast.error(error.data.exception, {position: toast.POSITION.BOTTOM_CENTER}) } catch(e){ - toast.error("Failed to save colormap", { position: toast.POSITION.BOTTOM_CENTER }); + toast.error("Failed to save colormap", { position: toast.POSITION.BOTTOM_CENTER }); } }) } @@ -222,11 +225,11 @@ class ColormapEditor extends Component { catch(e){ return false } - + } return new Promise((resolve, reject) => { try{ - /* eslint-disable no-undef */ + /* eslint-disable no-undef */ if(vcs){ vcs.getcolormapnames().then((names) => { if(names.indexOf(colormap_name) >= 0){ @@ -270,7 +273,7 @@ class ColormapEditor extends Component { } createNewColormapInVcs(base_cm, name){ - // create should copy the current colormap, save it into vcs, + // create should copy the current colormap, save it into vcs, // add it to redux and set it as active in the widget, and close the modal // cancel should close the modal try{ @@ -295,7 +298,7 @@ class ColormapEditor extends Component { toast.error("Failed to create colormap", {position: toast.POSITION.BOTTOM_CENTER}) } return Promise.resolve(false) - + } blendColors(){ @@ -319,7 +322,7 @@ class ColormapEditor extends Component { let blendedColormap = this.state.current_colormap.map(function(arr) { return arr.slice(); // copy inner array of colors }); - + for(let count = 1; currentCell < endCell; currentCell++, count++){ blendedColormap[currentCell][0] = startColor[0] + (redStep * count) blendedColormap[currentCell][1] = startColor[1] + (greenStep * count) @@ -361,7 +364,7 @@ class ColormapEditor extends Component {
-

- {this.handleChange(color)}} /> {this.handleCellClick(start_cell, end_cell)}} selected_cells_start={this.state.selected_cells_start} - selected_cells_end= {this.state.selected_cells_end} + selected_cells_end= {this.state.selected_cells_end} /> - - @@ -97,7 +139,7 @@ class ImportExportModal extends Component { type="file" onChange={(e)=>{this.handleFileChange(e)}}> -