-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy path3.+Renderers.snb
132 lines (132 loc) · 4.75 KB
/
3.+Renderers.snb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
"metadata":{
"name":"3. Renderers",
"user_save_timestamp":"2015-02-05T20:55:02.356Z",
"auto_save_timestamp":"2015-02-07T17:14:53.373Z"
},
"worksheets":[{
"cells":[{
"cell_type":"markdown",
"source":"## Renderers\n\nScala Notebook renders outputs automatically..."
},{
"cell_type":"code",
"input":"(1 to 100)",
"language":"scala",
"collapsed":false,
"prompt_number":9,
"outputs":[]
},{
"cell_type":"markdown",
"source":"But sometimes more granular control is required. In cases like this, one can use a custom Renderer to define a a custom visualization, in a type safe way."
},{
"cell_type":"code",
"input":"// First define an implicit renderer bullshit\nimplicit object overrideSeqRenderer extends Renderer[Seq[Int]] {\n def render(x: Seq[Int]) = widgets.text(x.take(25).mkString(\",\"))\n}",
"language":"scala",
"collapsed":false,
"prompt_number":16,
"outputs":[]
},{
"cell_type":"code",
"input":"(1 to 100)",
"language":"scala",
"collapsed":false,
"prompt_number":17,
"outputs":[]
},{
"cell_type":"code",
"input":"implicit object productRenderer extends Renderer[Product] {\n def render(x: Product) = widgets.row(\n (for { value <- x.productIterator.toSeq } yield widgets.text(value.toString)):_*\n )\n}",
"language":"scala",
"collapsed":false,
"prompt_number":12,
"outputs":[]
},{
"cell_type":"code",
"input":"case class Foo(a: Int, b: String)\nFoo(12, \"bjkdlf\")\n",
"language":"scala",
"collapsed":false,
"prompt_number":13,
"outputs":[]
},{
"cell_type":"code",
"input":"implicit object productRenderer extends Renderer[Seq[Product]] {\n def render(x: Seq[Product]) = {\n if (x.isEmpty) {\n widgets.text(\"[Empty seq]\")\n } else {\n val proto = x.head\n val values = x.flatMap(_.productIterator)\n widgets.layout(proto.productArity, \n values.map(v => widgets.text(v.toString))\n )\n }\n }\n}",
"language":"scala",
"collapsed":false,
"prompt_number":14,
"outputs":[]
},{
"cell_type":"code",
"input":"Seq(\n Foo(1, \"b\"), \n Foo(2, \"c\"), \n Foo(3, \"d\")\n)",
"language":"scala",
"collapsed":false,
"prompt_number":15,
"outputs":[]
},{
"cell_type":"code",
"input":"",
"language":"scala",
"collapsed":true,
"outputs":[]
}]
}],
"autosaved":[{
"cells":[{
"cell_type":"markdown",
"source":"## Renderers\n\nScala Notebook renders outputs automatically..."
},{
"cell_type":"code",
"input":"(1 to 100)",
"language":"scala",
"collapsed":false,
"prompt_number":17,
"outputs":[]
},{
"cell_type":"markdown",
"source":"But sometimes more granular control is required. In cases like this, one can use a custom Renderer to define a a custom visualization, in a type safe way."
},{
"cell_type":"code",
"input":"// First define an implicit renderer\nimplicit object overrideSeqRenderer extends Renderer[Seq[Int]] {\n def render(x: Seq[Int]) = widgets.text(x.take(25).mkString(\",\"))\n}",
"language":"scala",
"collapsed":false,
"prompt_number":18,
"outputs":[]
},{
"cell_type":"code",
"input":"(1 to 100)",
"language":"scala",
"collapsed":false,
"prompt_number":19,
"outputs":[]
},{
"cell_type":"code",
"input":"implicit object productRenderer extends Renderer[Product] {\n def render(x: Product) = widgets.row(\n (for { value <- x.productIterator.toSeq } yield widgets.text(value.toString)):_*\n )\n}",
"language":"scala",
"collapsed":false,
"outputs":[]
},{
"cell_type":"code",
"input":"case class Foo(a: Int, b: String)\nFoo(12, \"bjkdlf\")\n",
"language":"scala",
"collapsed":false,
"outputs":[]
},{
"cell_type":"code",
"input":"implicit object productRenderer extends Renderer[Seq[Product]] {\n def render(x: Seq[Product]) = {\n if (x.isEmpty) {\n widgets.text(\"[Empty seq]\")\n } else {\n val proto = x.head\n val values = x.flatMap(_.productIterator)\n widgets.layout(proto.productArity, \n values.map(v => widgets.text(v.toString))\n )\n }\n }\n}",
"language":"scala",
"collapsed":false,
"outputs":[]
},{
"cell_type":"code",
"input":"Seq(\n Foo(1, \"b\"), \n Foo(2, \"c\"), \n Foo(3, \"d\")\n)",
"language":"scala",
"collapsed":false,
"outputs":[]
},{
"cell_type":"code",
"input":"",
"language":"scala",
"collapsed":true,
"outputs":[]
}]
}],
"nbformat":3
}