Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility to store the data in an external file. #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion ipyvolume/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 = ""
Expand Down
1 change: 1 addition & 0 deletions ipyvolume/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
55 changes: 53 additions & 2 deletions js/src/scatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), {
Expand All @@ -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,
Expand All @@ -294,4 +345,4 @@ var ScatterModel = widgets.WidgetModel.extend({
module.exports = {
ScatterView:ScatterView,
ScatterModel:ScatterModel
}
}