Repository: altair-viz/altair
Pull request #239 (2016-10-20 03:33:48)

MAINT: add-in missing examples
Bug in the generation code was ignoring a number of examples. Also had to slightly tweak things to work correctly with explicitly specified data.


Patch:
diff --git a/altair/examples/example-listing.json b/altair/examples/example-listing.json
index 3567fc33..bff2b941 100644
--- a/altair/examples/example-listing.json
+++ b/altair/examples/example-listing.json
@@ -1,7 +1,7 @@
 {
   "Basic": [
     {
-      "name": "bar_1d",
+      "name": "bar",
       "title": "Simple Bar Chart",
       "galleryParameters": {
         "backgroundPosition": "25% 50%"
diff --git a/altair/examples/json/bar.vl.json b/altair/examples/json/bar.vl.json
new file mode 100644
index 00000000..92298830
--- /dev/null
+++ b/altair/examples/json/bar.vl.json
@@ -0,0 +1,54 @@
+{
+    "data": {
+        "values": [
+            {
+                "a": "A",
+                "b": 28
+            },
+            {
+                "a": "B",
+                "b": 55
+            },
+            {
+                "a": "C",
+                "b": 43
+            },
+            {
+                "a": "D",
+                "b": 91
+            },
+            {
+                "a": "E",
+                "b": 81
+            },
+            {
+                "a": "F",
+                "b": 53
+            },
+            {
+                "a": "G",
+                "b": 19
+            },
+            {
+                "a": "H",
+                "b": 87
+            },
+            {
+                "a": "I",
+                "b": 52
+            }
+        ]
+    },
+    "description": "A simple bar chart with embedded data.",
+    "encoding": {
+        "x": {
+            "field": "a",
+            "type": "ordinal"
+        },
+        "y": {
+            "field": "b",
+            "type": "quantitative"
+        }
+    },
+    "mark": "bar"
+}
\ No newline at end of file
diff --git a/altair/examples/json/bar_filter_calc.vl.json b/altair/examples/json/bar_filter_calc.vl.json
new file mode 100644
index 00000000..dead2306
--- /dev/null
+++ b/altair/examples/json/bar_filter_calc.vl.json
@@ -0,0 +1,63 @@
+{
+    "data": {
+        "values": [
+            {
+                "a": "A",
+                "b": 28
+            },
+            {
+                "a": "B",
+                "b": 55
+            },
+            {
+                "a": "C",
+                "b": 43
+            },
+            {
+                "a": "G",
+                "b": 19
+            },
+            {
+                "a": "H",
+                "b": 87
+            },
+            {
+                "a": "I",
+                "b": 52
+            },
+            {
+                "a": "D",
+                "b": 91
+            },
+            {
+                "a": "E",
+                "b": 81
+            },
+            {
+                "a": "F",
+                "b": 53
+            }
+        ]
+    },
+    "description": "A simple bar chart with embedded data that uses a filter and calculate.",
+    "encoding": {
+        "x": {
+            "field": "a",
+            "type": "ordinal"
+        },
+        "y": {
+            "field": "b2",
+            "type": "quantitative"
+        }
+    },
+    "mark": "bar",
+    "transform": {
+        "calculate": [
+            {
+                "expr": "2*datum.b",
+                "field": "b2"
+            }
+        ],
+        "filter": "datum.b2 > 60"
+    }
+}
\ No newline at end of file
diff --git a/altair/examples/json/bar_size_bandsize_small.vl.json b/altair/examples/json/bar_size_bandsize_small.vl.json
new file mode 100644
index 00000000..ae7c6057
--- /dev/null
+++ b/altair/examples/json/bar_size_bandsize_small.vl.json
@@ -0,0 +1,57 @@
+{
+    "data": {
+        "values": [
+            {
+                "a": "A",
+                "b": 28
+            },
+            {
+                "a": "B",
+                "b": 55
+            },
+            {
+                "a": "C",
+                "b": 43
+            },
+            {
+                "a": "D",
+                "b": 91
+            },
+            {
+                "a": "E",
+                "b": 81
+            },
+            {
+                "a": "F",
+                "b": 53
+            },
+            {
+                "a": "G",
+                "b": 19
+            },
+            {
+                "a": "H",
+                "b": 87
+            },
+            {
+                "a": "I",
+                "b": 52
+            }
+        ]
+    },
+    "description": "A simple bar chart with embedded data.",
+    "encoding": {
+        "x": {
+            "field": "a",
+            "scale": {
+                "bandSize": 11
+            },
+            "type": "ordinal"
+        },
+        "y": {
+            "field": "b",
+            "type": "quantitative"
+        }
+    },
+    "mark": "bar"
+}
\ No newline at end of file
diff --git a/altair/examples/json/field_spaces.vl.json b/altair/examples/json/field_spaces.vl.json
new file mode 100644
index 00000000..d46b9a5b
--- /dev/null
+++ b/altair/examples/json/field_spaces.vl.json
@@ -0,0 +1,57 @@
+{
+    "data": {
+        "values": [
+            {
+                "a b": 0.9228935641885049,
+                "c d": 0.5982771352733822
+            },
+            {
+                "a b": 0.6819537235661846,
+                "c d": 0.4599852852378238
+            },
+            {
+                "a b": 0.035533848836472814,
+                "c d": 0.6863884491167203
+            },
+            {
+                "a b": 0.3491133898504284,
+                "c d": 0.7368153745410643
+            },
+            {
+                "a b": 0.42475313594111874,
+                "c d": 0.8129620247637963
+            },
+            {
+                "a b": 0.5239172250991592,
+                "c d": 0.5616266265239936
+            },
+            {
+                "a b": 0.4668258532026234,
+                "c d": 0.436625706645942
+            },
+            {
+                "a b": 0.2692059264553106,
+                "c d": 0.2746758424896977
+            },
+            {
+                "a b": 0.6009825566170259,
+                "c d": 0.2648775327299746
+            },
+            {
+                "a b": 0.7457746080337384,
+                "c d": 0.15435369511584507
+            }
+        ]
+    },
+    "encoding": {
+        "x": {
+            "field": "a b",
+            "type": "quantitative"
+        },
+        "y": {
+            "field": "c d",
+            "type": "quantitative"
+        }
+    },
+    "mark": "point"
+}
\ No newline at end of file
diff --git a/altair/examples/json/layer_bar_line.vl.json b/altair/examples/json/layer_bar_line.vl.json
new file mode 100644
index 00000000..1aca8ec8
--- /dev/null
+++ b/altair/examples/json/layer_bar_line.vl.json
@@ -0,0 +1,73 @@
+{
+    "data": {
+        "values": [
+            {
+                "a": "A",
+                "b": 28
+            },
+            {
+                "a": "B",
+                "b": 55
+            },
+            {
+                "a": "C",
+                "b": 43
+            },
+            {
+                "a": "D",
+                "b": 91
+            },
+            {
+                "a": "E",
+                "b": 81
+            },
+            {
+                "a": "F",
+                "b": 53
+            },
+            {
+                "a": "G",
+                "b": 19
+            },
+            {
+                "a": "H",
+                "b": 87
+            },
+            {
+                "a": "I",
+                "b": 52
+            }
+        ]
+    },
+    "layers": [
+        {
+            "encoding": {
+                "x": {
+                    "field": "a",
+                    "type": "ordinal"
+                },
+                "y": {
+                    "field": "b",
+                    "type": "quantitative"
+                }
+            },
+            "mark": "bar"
+        },
+        {
+            "encoding": {
+                "color": {
+                    "value": "red"
+                },
+                "x": {
+                    "field": "a",
+                    "type": "ordinal"
+                },
+                "y": {
+                    "field": "b",
+                    "type": "quantitative"
+                }
+            },
+            "mark": "line"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/altair/examples/json/layer_bar_line_union.vl.json b/altair/examples/json/layer_bar_line_union.vl.json
new file mode 100644
index 00000000..a902a1ac
--- /dev/null
+++ b/altair/examples/json/layer_bar_line_union.vl.json
@@ -0,0 +1,82 @@
+{
+    "data": {
+        "values": [
+            {
+                "a": "A",
+                "b": "B",
+                "c": 28
+            },
+            {
+                "a": "B",
+                "b": "C",
+                "c": 55
+            },
+            {
+                "a": "C",
+                "b": "D",
+                "c": 43
+            },
+            {
+                "a": "D",
+                "b": "E",
+                "c": 91
+            },
+            {
+                "a": "E",
+                "b": "F",
+                "c": 81
+            },
+            {
+                "a": "F",
+                "b": "G",
+                "c": 53
+            },
+            {
+                "a": "G",
+                "b": "H",
+                "c": 19
+            },
+            {
+                "a": "H",
+                "b": "I",
+                "c": 87
+            },
+            {
+                "a": "I",
+                "b": "J",
+                "c": 52
+            }
+        ]
+    },
+    "layers": [
+        {
+            "encoding": {
+                "x": {
+                    "field": "a",
+                    "type": "ordinal"
+                },
+                "y": {
+                    "field": "c",
+                    "type": "quantitative"
+                }
+            },
+            "mark": "bar"
+        },
+        {
+            "encoding": {
+                "color": {
+                    "value": "red"
+                },
+                "x": {
+                    "field": "b",
+                    "type": "ordinal"
+                },
+                "y": {
+                    "field": "c",
+                    "type": "quantitative"
+                }
+            },
+            "mark": "line"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/altair/examples/json/minimal.vl.json b/altair/examples/json/minimal.vl.json
new file mode 100644
index 00000000..c1b37afc
--- /dev/null
+++ b/altair/examples/json/minimal.vl.json
@@ -0,0 +1,8 @@
+{
+    "data": {
+        "values": [
+            {}
+        ]
+    },
+    "mark": "point"
+}
\ No newline at end of file
diff --git a/altair/examples/json/point_color.vl.json b/altair/examples/json/point_color.vl.json
new file mode 100644
index 00000000..eb83d36d
--- /dev/null
+++ b/altair/examples/json/point_color.vl.json
@@ -0,0 +1,16 @@
+{
+    "config": {
+        "mark": {
+            "color": "purple"
+        }
+    },
+    "data": {
+        "values": [
+            {
+                "a": 2
+            }
+        ]
+    },
+    "encoding": {},
+    "mark": "point"
+}
\ No newline at end of file
diff --git a/altair/examples/json/point_ordinal_color.vl.json b/altair/examples/json/point_ordinal_color.vl.json
new file mode 100644
index 00000000..501bfd10
--- /dev/null
+++ b/altair/examples/json/point_ordinal_color.vl.json
@@ -0,0 +1,51 @@
+{
+    "data": {
+        "values": [
+            {
+                "a": "B",
+                "x": 2,
+                "y": 2
+            },
+            {
+                "a": "A",
+                "x": 1,
+                "y": 1
+            },
+            {
+                "a": "A",
+                "x": 4,
+                "y": 4
+            },
+            {
+                "a": "B",
+                "x": 5,
+                "y": 5
+            },
+            {
+                "a": "C",
+                "x": 3,
+                "y": 3
+            },
+            {
+                "a": "C",
+                "x": 6,
+                "y": 6
+            }
+        ]
+    },
+    "encoding": {
+        "color": {
+            "field": "a",
+            "type": "ordinal"
+        },
+        "x": {
+            "field": "x",
+            "type": "quantitative"
+        },
+        "y": {
+            "field": "y",
+            "type": "quantitative"
+        }
+    },
+    "mark": "point"
+}
\ No newline at end of file
diff --git a/altair/examples/json/scatter_log.vl.json b/altair/examples/json/scatter_log.vl.json
new file mode 100644
index 00000000..6d57eae6
--- /dev/null
+++ b/altair/examples/json/scatter_log.vl.json
@@ -0,0 +1,52 @@
+{
+    "data": {
+        "values": [
+            {
+                "x": 0,
+                "y": 1
+            },
+            {
+                "x": 1,
+                "y": 10
+            },
+            {
+                "x": 2,
+                "y": 100
+            },
+            {
+                "x": 3,
+                "y": 1000
+            },
+            {
+                "x": 4,
+                "y": 10000
+            },
+            {
+                "x": 5,
+                "y": 100000
+            },
+            {
+                "x": 6,
+                "y": 1000000
+            },
+            {
+                "x": 7,
+                "y": 10000000
+            }
+        ]
+    },
+    "encoding": {
+        "x": {
+            "field": "x",
+            "type": "quantitative"
+        },
+        "y": {
+            "field": "y",
+            "scale": {
+                "type": "log"
+            },
+            "type": "quantitative"
+        }
+    },
+    "mark": "point"
+}
\ No newline at end of file
diff --git a/altair/utils/visitors.py b/altair/utils/visitors.py
index 56ac172b..5ebc0b36 100644
--- a/altair/utils/visitors.py
+++ b/altair/utils/visitors.py
@@ -176,11 +176,7 @@ class FromDict(Visitor):
         # Remove data first and handle it specially later
         if 'data' in dct:
             dct = dct.copy()
-            data = dct.pop('data')
-            if set(data.keys()) == {'values'}:
-                data = pd.DataFrame(data['values'])
-            else:
-                data = self.clsvisit_BaseObject(schema.Data, data)
+            data = self.clsvisit(schema.Data, dct.pop('data'))
             obj = self.clsvisit_BaseObject(cls, dct)
             obj.data = data
         else:
@@ -191,6 +187,13 @@ class FromDict(Visitor):
     clsvisit_LayeredChart = _clsvisit_with_data
     clsvisit_FacetedChart = _clsvisit_with_data
 
+    def clsvisit_Data(self, trait, dct, *args, **kwargs):
+        dct = dct.copy()
+        values = dct.pop('values', None)
+        obj = self.clsvisit_BaseObject(trait, dct, *args, **kwargs)
+        obj.values = values
+        return obj
+
     def visit_List(self, trait, dct, *args, **kwargs):
         return [self.visit(trait._trait, item) for item in dct]
 
diff --git a/tools/sync_vegalite.py b/tools/sync_vegalite.py
index 79bcaffc..a1256d3a 100644
--- a/tools/sync_vegalite.py
+++ b/tools/sync_vegalite.py
@@ -101,12 +101,9 @@ def sync_examples(vegalite_repo,
             spec = json.load(f)
 
         # Adjust to use absolute URLs
-        try:
-            url = spec['data']['url']
-            if url.startswith('data/'):
-                spec['data']['url'] = data_url + url
-        except KeyError:
-            continue
+        url = spec['data'].get('url', '')
+        if url.startswith('data/'):
+            spec['data']['url'] = data_url + url
 
         print(" -> {0}".format(destination_file))
         with open(destination_file, 'w') as f:


Test patch:
diff --git a/altair/examples/tests/test_examples.py b/altair/examples/tests/test_examples.py
index 37b84771..6d325a1c 100644
--- a/altair/examples/tests/test_examples.py
+++ b/altair/examples/tests/test_examples.py
@@ -6,6 +6,11 @@ from .. import iter_examples, load_example, iter_examples_with_metadata
 from ... import *
 
 
+def remove_empty_fields(spec):
+    """Remove empty entries from the spec"""
+    return {k:v for k, v in spec.items() if v}
+
+
 @pytest.mark.parametrize('example', iter_examples())
 def test_json_examples_round_trip(example):
     """
@@ -17,8 +22,10 @@ def test_json_examples_round_trip(example):
     v_dict = v.to_dict()
     assert v_dict == json_dict
 
+    # code generation discards empty function calls, and so we
+    # filter these out before comparison
     v2 = eval(v.to_altair())
-    assert v_dict == v2.to_dict()
+    assert v2.to_dict() == remove_empty_fields(json_dict)
 
 
 def test_load_example():
@@ -34,5 +41,3 @@ def test_metadata(D):
     expected_keys = {'spec', 'filename', 'category', 'name', 'title'}
     assert len(expected_keys - set(D.keys())) == 0
     assert 'data' in D['spec']
-    
-