File size: 3,796 Bytes
150e09e
b329c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150e09e
 
b329c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150e09e
 
b329c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<script lang="ts">
	import Dataframe from '@gradio/dataframe'
	import { onMount } from 'svelte'

	let value = $state({
		data: [
			['Alice', 25, true, 'Engineer'],
			['Bob', 30, false, 'Designer'],
			['Charlie', 35, true, 'Manager'],
			['Diana', 28, true, 'Developer'],
			['Eve', 22, false, 'Intern']
		],
		headers: ['Name', 'Age', 'Active', 'Role']
	})

	let datatype = $state(['str', 'number', 'bool', 'str'])
	let show_search = $state<'search' | 'filter' | 'none'>('search')
	let editable = $state(true)
	let show_row_numbers = $state(true)
	let show_copy_button = $state(true)
	let show_fullscreen_button = $state(true)
	let max_height = $state(400)
	let max_chars = $state(50)
	let line_breaks = $state(true)
	let wrap = $state(false)
	let column_widths = $state<string[]>([])

	function handle_change(e: CustomEvent) {
		console.log('Data changed:', e.detail)
		value = e.detail
	}

	function handle_select(e: CustomEvent) {
		console.log('Cell selected:', e.detail)
	}

	function handle_input(e: CustomEvent) {
		console.log('Search input:', e.detail)
	}

	function toggleEditable() {
		editable = !editable
	}

	function toggleRowNumbers() {
		show_row_numbers = !show_row_numbers
	}

	function toggleCopyButton() {
		show_copy_button = !show_copy_button
	}

	function toggleFullscreenButton() {
		show_fullscreen_button = !show_fullscreen_button
	}

	function updateColumnWidths() {
		if (column_widths.length > 0) {
			column_widths = []
		} else {
			column_widths = ['150px', '100px', '100px', '200px']
		}
	}

	onMount(() => {
		// Add more sample data for virtualization demo
		const additionalData = Array.from({ length: 100 }, (_, i) => [
			`User ${i + 6}`,
			Math.floor(Math.random() * 50) + 20,
			Math.random() > 0.5,
			['Engineer', 'Designer', 'Manager', 'Developer', 'Intern'][Math.floor(Math.random() * 5)]
		])
		value.data = [...value.data, ...additionalData]
	})
</script>

<div class="container">
	<h1>@gradio/dataframe Demo</h1>
	
	<div class="controls">
		<div class="control-group">
			<label>
				<input type="checkbox" bind:checked={editable} />
				Editable
			</label>
			<label>
				<input type="checkbox" bind:checked={show_row_numbers} />
				Show Row Numbers
			</label>
			<label>
				<input type="checkbox" bind:checked={show_copy_button} />
				Show Copy Button
			</label>
			<label>
				<input type="checkbox" bind:checked={show_fullscreen_button} />
				Show Fullscreen Button
			</label>
			<button on:click={updateColumnWidths}>
				{column_widths.length > 0 ? 'Reset Column Widths' : 'Set Column Widths'}
			</button>
		</div>
	</div>

	<div class="df-theme">
		<Dataframe
			bind:value
			{datatype}
			{show_search}
			{editable}
			{show_row_numbers}
			{show_copy_button}
			{show_fullscreen_button}
			{max_height}
			{max_chars}
			{line_breaks}
			{wrap}
			{column_widths}
			on:change={handle_change}
			on:select={handle_select}
			on:input={handle_input}
		/>
	</div>
</div>

<style>
	.container {
		max-width: 1200px;
		margin: 0 auto;
		padding: 2rem;
	}

	h1 {
		color: #7c3aed;
		margin-bottom: 2rem;
	}

	.controls {
		margin-bottom: 2rem;
		padding: 1rem;
		background-color: #f5f5f5;
		border-radius: 8px;
	}

	.control-group {
		display: flex;
		flex-wrap: wrap;
		gap: 1rem;
		justify-content: center;
		align-items: center;
	}

	label {
		display: flex;
		align-items: center;
		gap: 0.5rem;
		font-weight: 500;
	}

	button {
		padding: 0.5rem 1rem;
		background-color: #7c3aed;
		color: white;
		border: none;
		border-radius: 4px;
		cursor: pointer;
		font-weight: 500;
		transition: background-color 0.2s;
	}

	button:hover {
		background-color: #6d28d9;
	}

	@media (max-width: 768px) {
		.container {
			padding: 1rem;
		}
		
		.control-group {
			flex-direction: column;
			align-items: flex-start;
		}
	}
</style>