diff --git a/ipyvolume/embed.py b/ipyvolume/embed.py index 5185038b..945200d9 100644 --- a/ipyvolume/embed.py +++ b/ipyvolume/embed.py @@ -88,7 +88,7 @@ def add_referring_widgets(states, drop_defaults=False): #get_state(value, store, drop_defaults=drop_defaults) return found_new -def embed_html(filename, widgets, drop_defaults=False, all=False, title="ipyvolume embed example", template=template, **kwargs): +def embed_html(filename, widgets, drop_defaults=False, all=False, title="ipyvolume embed example", template=template, inside=False, **kwargs): try: widgets[0] except (IndexError, TypeError): @@ -110,6 +110,31 @@ def embed_html(filename, widgets, drop_defaults=False, all=False, title="ipyvolu finally: ipyvolume.serialize.performance = previous + # get the module that we will store in other file: + for k in state.keys(): + if "IPY_MODEL" in state[k]["state"].get("layout", "") and \ + "ScatterView" in state[k]["state"].get("_view_name", ""): + if state[k]["state"]["embed"] is None or inside: + # Do nothing + continue + + if state[k]["state"]["embed"] == "": + # Generate automaticaly a "unique" name for the json + filename = "data_" + state[k]["state"]["layout"].split("_")[-1] + ".json" + state[k]["state"]["embed"] = filename + else: + # or give it the name defined by user + filename = state[k]["state"]["embed"] + + # Get the data + data = ["x","y","z","vx","vy","vz","color","sequence_index"] + json_data = {"data": {}} + for key in data: + json_data["data"][key] = state[k]["state"].pop(key) + + with open(filename, "w") as json_f: + json_f.write(json.dumps(json_data)) + values = dict(extra_script_head="", body_pre="", body_post="") values.update(kwargs) widget_views = "" diff --git a/ipyvolume/volume.py b/ipyvolume/volume.py index c915cc0f..5a571661 100644 --- a/ipyvolume/volume.py +++ b/ipyvolume/volume.py @@ -22,6 +22,7 @@ class Scatter(widgets.DOMWidget): _view_module = Unicode('ipyvolume').tag(sync=True) _model_name = Unicode('ScatterModel').tag(sync=True) _model_module = Unicode('ipyvolume').tag(sync=True) + embed = Unicode('', allow_none=True).tag(sync=True) x = Array(default_value=None).tag(sync=True, **create_array_binary_serialization('x')) y = Array(default_value=None).tag(sync=True, **create_array_binary_serialization('y')) z = Array(default_value=None).tag(sync=True, **create_array_binary_serialization('z')) diff --git a/js/src/scatter.js b/js/src/scatter.js index a5c3db44..c4e2b839 100644 --- a/js/src/scatter.js +++ b/js/src/scatter.js @@ -257,6 +257,16 @@ var ScatterView = widgets.WidgetView.extend( { } }); +var Data = Backbone.Model.extend(); + var ValueCollection = Backbone.Collection.extend({ + model: Data, + url: "data.json", + parse: function (response) { + return response.data + } + }); + + var ScatterModel = widgets.WidgetModel.extend({ defaults: function() { return _.extend(widgets.WidgetModel.prototype.defaults(), { @@ -273,7 +283,48 @@ var ScatterModel = widgets.WidgetModel.extend({ geo: 'diamond', sequence_index: 0 }) - }}, { + }, + initialize : function(options) { + console.log("Initialize") + ScatterModel.__super__.initialize.apply(this, arguments); + + var loading = new ValueCollection () + //loading.fetch() + if ( !( _.isEmpty(this.get("embed")) || typeof this.get("embed") == "undefined")){ + loading.url = this.get("embed") + //Provide default value to start the rendering meanwhile + this.set("x",serialize.deserialize_array_or_json([[0,0,1],[0,0,1]])) + this.set("y",serialize.deserialize_array_or_json([[1,0,0],[0,0,1]])) + this.set("z",serialize.deserialize_array_or_json([[0,0,0],[0,0,0]])) + + loading.fetch( ) + } + this.listenToOnce(loading, "sync", function(){ + var data = loading.toJSON()[0] + //console.log(data, data["x"]) + to_load = ["x","y","z","vx","vy","vz","color","sequence_index"] + + _.each(to_load , function(attribute){ + console.log(attribute) + if (typeof data[attribute] != "undefined"){ + //Get the different types + var loadedValue + if (attribute == "color") + loadedValue = serialize.deserialize_color_or_json(data[attribute]) + else + loadedValue = serialize.deserialize_array_or_json(data[attribute]) + + console.log(loadedValue) + this.set({[attribute]: loadedValue}) + } + },this) + //console.log(this.get("x")) + + //console.log("syinc") + }) + this.set({loading:loading}) + } + }, { serializers: _.extend({ x: serialize.array_or_json, y: serialize.array_or_json, @@ -294,4 +345,4 @@ var ScatterModel = widgets.WidgetModel.extend({ module.exports = { ScatterView:ScatterView, ScatterModel:ScatterModel -} \ No newline at end of file +}