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 # gradio_leaderboard
3
+
4
+ ## 🔋⚡️🥇 Super fast, batteries included Leaderboards with minimal code.
5
+ <img alt="Static Badge" src="https://img.shields.io/badge/version%20-%200.0.2%20-%20orange">
6
+
7
+
8
+ The `gradio_leaderboard` package helps you build fully functional and performant leaderboard demos with `gradio`.
9
+
10
+ Place the `gradio_leaderboard.Leaderboard` component anywhere in your Gradio application (and optionally pass in some configuration). That's it!
11
+
12
+ For example usage, please see the [Usage](#usage) section.
13
+
14
+ For details on configuration, please see the [Configuration](#configuration) section.
15
+
16
+ For the API reference, see the [Initialization](#initialization) section.
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ pip install gradio_leaderboard
22
+ ```
23
+
24
+ or add `gradio_leaderboard` to your `requirements.txt`.
25
+
26
+ ## Usage
27
+
28
+ ```python
29
+
30
+ import gradio as gr
31
+ from gradio_leaderboard import Leaderboard
32
+ from pathlib import Path
33
+ import pandas as pd
34
+
35
+ abs_path = Path(__file__).parent
36
+
37
+ # Any pandas-compatible data
38
+ df = pd.read_json(str(abs_path / "leaderboard_data.json"))
39
+
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("""
42
+ # 🥇 Leaderboard Component
43
+ """)
44
+ Leaderboard(
45
+ value=df,
46
+ select_columns=["T", "Model", "Average ⬆️", "ARC",
47
+ "HellaSwag", "MMLU", "TruthfulQA",
48
+ "Winogrande", "GSM8K"],
49
+ search_columns=["model_name_for_query", "Type"],
50
+ hide_columns=["model_name_for_query", "Model Size"],
51
+ filter_columns=["T", "Precision", "Model Size"],
52
+ )
53
+
54
+ if __name__ == "__main__":
55
+ demo.launch()
56
+ ```
57
+
58
+ ## Configuration
59
+
60
+ ### Selecting
61
+
62
+ When column selection is enabled, a checkboxgroup will be displayed in the top left corner of the leaderboard that lets users
63
+ select which columns are displayed.
64
+
65
+ You can disable/configure the column selection behavior of the `Leaderboard` with the `select_columns` parameter.
66
+ It's value can be:
67
+
68
+ * `None`: Column selection is not allowed and all of the columns are displayed when the leaderboard loads.
69
+ * `list of column names`: All columns can be selected and the elements of this list correspond to the initial set of selected columns.
70
+ * `SelectColumns instance`: You can import `SelectColumns` from `gradio_leaderboard` for full control of the column selection behavior as well as the checkboxgroup appearance. See an example below.
71
+
72
+ #### Demo
73
+
74
+ ```python
75
+ import pandas as pd
76
+ import gradio as gr
77
+ from gradio_leaderboard import Leaderboard, SelectColumns
78
+
79
+ with gr.Blocks() as demo:
80
+ Leaderboard(
81
+ value=pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}),
82
+ select_columns=SelectColumns(default_selection=["a", "b"],
83
+ cant_deselect="a",
84
+ label="Select The Columns",
85
+ info="Helpful information")
86
+ )
87
+
88
+ demo.launch()
89
+ ```
90
+
91
+ ![](https://github.com/freddyaboulton/gradio-leaderboard/assets/41651716/ea073681-c01e-4d40-814c-1f3cd56ef292)
92
+
93
+
94
+ ### Searching
95
+
96
+ When searching is enabled, a textbox will appear in the top left corner of the leaderboard.
97
+ Users will be able to display rows that match their search query.
98
+
99
+ Searching follows the following rules:
100
+
101
+ 1. Multiple queries can be separated by a semicolon `;`.
102
+ 2. Any subquery is matched against the `primary search column` by default.
103
+ 3. To match against a `secondary search column`, the query must be preceded by the column name and a colon (`:`), e.g. `Name: Maria`.
104
+ 4. The returned rows are those that match against `ANY` primary search column and `ALL` secondary search columns.
105
+
106
+ You can configure searching with the `search_columns` parameter. It's value can be:
107
+ * `a list`: In which case the first element is the `primary search column` and the remaining are the `secondary search columns`.
108
+ * A `SearchColumns` instance. This lets you specify the primary and secondary columns explicitly as well as customize the search textbox appearance.
109
+
110
+
111
+ #### Demo
112
+
113
+ ```python
114
+ import pandas as pd
115
+ import gradio as gr
116
+ from gradio_leaderboard import Leaderboard, SearchColumns
117
+
118
+ with gr.Blocks() as demo:
119
+ Leaderboard(
120
+ value=pd.DataFrame({"name": ["Freddy", "Maria", "Mark"], "country": ["USA", "Mexico", "USA"]}),
121
+ search_columns=SearchColumns(primary_column="name", secondary_columns="country",
122
+ placeholder="Search by name or country. To search by country, type 'country:<query>'",
123
+ label="Search"),
124
+ )
125
+
126
+ demo.launch()
127
+ ```
128
+
129
+ ![](https://github.com/freddyaboulton/gradio-leaderboard/assets/41651716/032a50ef-01e0-48c4-a323-c84d09ccb3db)
130
+
131
+
132
+ ### Filtering
133
+
134
+ You can let users filter out rows from the leaderboard with the `filter_columns` parameter.
135
+ This will display a series of form elements that users can use to select/deselect which rows are displayed.
136
+
137
+ This parameter must be a `list` but it's elements must be:
138
+
139
+ * `a string`: Corresponding to the column name you'd like to add a filter for
140
+ * `a ColumnFilter`: A special class for full control of the filter's type, e.g. `checkboxgroup`, `slider`, or `dropdown`, as well as it's appearance in the UI.
141
+
142
+ If the `type` of the `ColumnFilter` is not specified, a heuristic will be used to choose the most appropriate type. If the data in the column is numeric, a slider will be used. If not, a `checkboxgroup` will be used.
143
+
144
+
145
+ #### Demo
146
+
147
+ ```python
148
+ import pandas as pd
149
+ import gradio as gr
150
+ from gradio_leaderboard import Leaderboard, ColumnFilter
151
+
152
+ with gr.Blocks() as demo:
153
+ Leaderboard(
154
+ value=pd.DataFrame({"name": ["Freddy", "Maria", "Mark"], "country": ["USA", "Mexico", "USA"],
155
+ "age": [25, 30, 35], "score": [100, 200, 300]}),
156
+ filter_columns=[
157
+ "name",
158
+ ColumnFilter("country", type="dropdown", label="Select Country 🇺🇸🇲🇽"),
159
+ ColumnFilter("age", type="slider", min=20, max=40, greater_than=True),
160
+ ColumnFilter("score", type="slider", min=50, max=350, greater_than=True)],
161
+ )
162
+
163
+ demo.launch()
164
+ ```
165
+
166
+ ![column_filter_gif](https://github.com/freddyaboulton/gradio-leaderboard/assets/41651716/24314762-6719-473e-be07-86aa50ed2bf1)
167
+
168
+ ## `Leaderboard`
169
+
170
+ ### Initialization
171
+
172
+ <table>
173
+ <thead>
174
+ <tr>
175
+ <th align="left">name</th>
176
+ <th align="left" style="width: 25%;">type</th>
177
+ <th align="left">default</th>
178
+ <th align="left">description</th>
179
+ </tr>
180
+ </thead>
181
+ <tbody>
182
+ <tr>
183
+ <td align="left"><code>value</code></td>
184
+ <td align="left" style="width: 25%;">
185
+
186
+ ```python
187
+ pd.DataFrame | None
188
+ ```
189
+
190
+ </td>
191
+ <td align="left"><code>None</code></td>
192
+ <td align="left">Default value to display in the DataFrame. If a Styler is provided, it will be used to set the displayed value in the DataFrame (e.g. to set precision of numbers) if the `interactive` is False. If a Callable function is provided, the function will be called whenever the app loads to set the initial value of the component.</td>
193
+ </tr>
194
+
195
+ <tr>
196
+ <td align="left"><code>datatype</code></td>
197
+ <td align="left" style="width: 25%;">
198
+
199
+ ```python
200
+ str | list[str]
201
+ ```
202
+
203
+ </td>
204
+ <td align="left"><code>"str"</code></td>
205
+ <td align="left">Datatype of values in sheet. Can be provided per column as a list of strings, or for the entire sheet as a single string. Valid datatypes are "str", "number", "bool", "date", and "markdown".</td>
206
+ </tr>
207
+
208
+ <tr>
209
+ <td align="left"><code>search_columns</code></td>
210
+ <td align="left" style="width: 25%;">
211
+
212
+ ```python
213
+ list[str] | SearchColumns
214
+ ```
215
+
216
+ </td>
217
+ <td align="left"><code>None</code></td>
218
+ <td align="left">See Configuration section of docs for details.</td>
219
+ </tr>
220
+
221
+ <tr>
222
+ <td align="left"><code>select_columns</code></td>
223
+ <td align="left" style="width: 25%;">
224
+
225
+ ```python
226
+ list[str] | SelectColumns
227
+ ```
228
+
229
+ </td>
230
+ <td align="left"><code>None</code></td>
231
+ <td align="left">See Configuration section of docs for details.</td>
232
+ </tr>
233
+
234
+ <tr>
235
+ <td align="left"><code>filter_columns</code></td>
236
+ <td align="left" style="width: 25%;">
237
+
238
+ ```python
239
+ list[str | ColumnFilter] | None
240
+ ```
241
+
242
+ </td>
243
+ <td align="left"><code>None</code></td>
244
+ <td align="left">See Configuration section of docs for details.</td>
245
+ </tr>
246
+
247
+ <tr>
248
+ <td align="left"><code>hide_columns</code></td>
249
+ <td align="left" style="width: 25%;">
250
+
251
+ ```python
252
+ list[str] | None
253
+ ```
254
+
255
+ </td>
256
+ <td align="left"><code>None</code></td>
257
+ <td align="left">List of columns to hide by default. They will not be displayed in the table but they can still be used for searching, filtering.</td>
258
+ </tr>
259
+
260
+ <tr>
261
+ <td align="left"><code>latex_delimiters</code></td>
262
+ <td align="left" style="width: 25%;">
263
+
264
+ ```python
265
+ list[dict[str, str | bool]] | None
266
+ ```
267
+
268
+ </td>
269
+ <td align="left"><code>None</code></td>
270
+ <td align="left">A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html). Only applies to columns whose datatype is "markdown".</td>
271
+ </tr>
272
+
273
+ <tr>
274
+ <td align="left"><code>label</code></td>
275
+ <td align="left" style="width: 25%;">
276
+
277
+ ```python
278
+ str | None
279
+ ```
280
+
281
+ </td>
282
+ <td align="left"><code>None</code></td>
283
+ <td align="left">The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.</td>
284
+ </tr>
285
+
286
+ <tr>
287
+ <td align="left"><code>show_label</code></td>
288
+ <td align="left" style="width: 25%;">
289
+
290
+ ```python
291
+ bool | None
292
+ ```
293
+
294
+ </td>
295
+ <td align="left"><code>None</code></td>
296
+ <td align="left">if True, will display label.</td>
297
+ </tr>
298
+
299
+ <tr>
300
+ <td align="left"><code>every</code></td>
301
+ <td align="left" style="width: 25%;">
302
+
303
+ ```python
304
+ float | None
305
+ ```
306
+
307
+ </td>
308
+ <td align="left"><code>None</code></td>
309
+ <td align="left">If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.</td>
310
+ </tr>
311
+
312
+ <tr>
313
+ <td align="left"><code>height</code></td>
314
+ <td align="left" style="width: 25%;">
315
+
316
+ ```python
317
+ int
318
+ ```
319
+
320
+ </td>
321
+ <td align="left"><code>500</code></td>
322
+ <td align="left">The maximum height of the dataframe, specified in pixels if a number is passed, or in CSS units if a string is passed. If more rows are created than can fit in the height, a scrollbar will appear.</td>
323
+ </tr>
324
+
325
+ <tr>
326
+ <td align="left"><code>scale</code></td>
327
+ <td align="left" style="width: 25%;">
328
+
329
+ ```python
330
+ int | None
331
+ ```
332
+
333
+ </td>
334
+ <td align="left"><code>None</code></td>
335
+ <td align="left">relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.</td>
336
+ </tr>
337
+
338
+ <tr>
339
+ <td align="left"><code>min_width</code></td>
340
+ <td align="left" style="width: 25%;">
341
+
342
+ ```python
343
+ int
344
+ ```
345
+
346
+ </td>
347
+ <td align="left"><code>160</code></td>
348
+ <td align="left">minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.</td>
349
+ </tr>
350
+
351
+ <tr>
352
+ <td align="left"><code>interactive</code></td>
353
+ <td align="left" style="width: 25%;">
354
+
355
+ ```python
356
+ bool | None
357
+ ```
358
+
359
+ </td>
360
+ <td align="left"><code>None</code></td>
361
+ <td align="left">if True, will allow users to edit the dataframe; if False, can only be used to display data. If not provided, this is inferred based on whether the component is used as an input or output.</td>
362
+ </tr>
363
+
364
+ <tr>
365
+ <td align="left"><code>visible</code></td>
366
+ <td align="left" style="width: 25%;">
367
+
368
+ ```python
369
+ bool
370
+ ```
371
+
372
+ </td>
373
+ <td align="left"><code>True</code></td>
374
+ <td align="left">If False, component will be hidden.</td>
375
+ </tr>
376
+
377
+ <tr>
378
+ <td align="left"><code>elem_id</code></td>
379
+ <td align="left" style="width: 25%;">
380
+
381
+ ```python
382
+ str | None
383
+ ```
384
+
385
+ </td>
386
+ <td align="left"><code>None</code></td>
387
+ <td align="left">An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
388
+ </tr>
389
+
390
+ <tr>
391
+ <td align="left"><code>elem_classes</code></td>
392
+ <td align="left" style="width: 25%;">
393
+
394
+ ```python
395
+ list[str] | str | None
396
+ ```
397
+
398
+ </td>
399
+ <td align="left"><code>None</code></td>
400
+ <td align="left">An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
401
+ </tr>
402
+
403
+ <tr>
404
+ <td align="left"><code>render</code></td>
405
+ <td align="left" style="width: 25%;">
406
+
407
+ ```python
408
+ bool
409
+ ```
410
+
411
+ </td>
412
+ <td align="left"><code>True</code></td>
413
+ <td align="left">If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.</td>
414
+ </tr>
415
+
416
+ <tr>
417
+ <td align="left"><code>wrap</code></td>
418
+ <td align="left" style="width: 25%;">
419
+
420
+ ```python
421
+ bool
422
+ ```
423
+
424
+ </td>
425
+ <td align="left"><code>False</code></td>
426
+ <td align="left">If True, the text in table cells will wrap when appropriate. If False and the `column_width` parameter is not set, the column widths will expand based on the cell contents and the table may need to be horizontally scrolled. If `column_width` is set, then any overflow text will be hidden.</td>
427
+ </tr>
428
+
429
+ <tr>
430
+ <td align="left"><code>line_breaks</code></td>
431
+ <td align="left" style="width: 25%;">
432
+
433
+ ```python
434
+ bool
435
+ ```
436
+
437
+ </td>
438
+ <td align="left"><code>True</code></td>
439
+ <td align="left">If True (default), will enable Github-flavored Markdown line breaks in chatbot messages. If False, single new lines will be ignored. Only applies for columns of type "markdown."</td>
440
+ </tr>
441
+
442
+ <tr>
443
+ <td align="left"><code>column_widths</code></td>
444
+ <td align="left" style="width: 25%;">
445
+
446
+ ```python
447
+ list[str | int] | None
448
+ ```
449
+
450
+ </td>
451
+ <td align="left"><code>None</code></td>
452
+ <td align="left">An optional list representing the width of each column. The elements of the list should be in the format "100px" (ints are also accepted and converted to pixel values) or "10%". If not provided, the column widths will be automatically determined based on the content of the cells. Setting this parameter will cause the browser to try to fit the table within the page width.</td>
453
+ </tr>
454
+ </tbody></table>