/** * KeplerGL widget entry point. * * KeplerGL is a React-based visualization library. This widget provides * a simple display showing the configured datasets and settings. * For full interactivity, use the to_html() export method. */ import type { AnyModel } from '@anywidget/types'; interface KeplerGLModel extends AnyModel { get(key: 'center'): [number, number]; get(key: 'zoom'): number; get(key: 'width'): string; get(key: 'height'): string; get(key: 'config'): Record; get(key: 'datasets'): Record; get(key: 'mapbox_token'): string; get(key: 'read_only'): boolean; get(key: '_js_calls'): Array<{ id: number; method: string; args: unknown[]; kwargs: Record }>; } /** * Format bytes to human readable string. */ function formatBytes(bytes: number): string { if (bytes < 1024) return bytes + ' B'; if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; } /** * Create the widget content. */ function createWidgetContent(model: KeplerGLModel): HTMLElement { const container = document.createElement('div'); container.style.cssText = ` font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); color: #fff; padding: 24px; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); `; const center = model.get('center') || [-122.4, 37.8]; const zoom = model.get('zoom') || 10; const datasets = model.get('datasets') || {}; const readOnly = model.get('read_only') || false; const datasetCount = Object.keys(datasets).length; let totalDataSize = 0; const datasetItems = Object.entries(datasets).map(([id, dataset]: [string, any]) => { const dataStr = JSON.stringify(dataset); totalDataSize += dataStr.length; const rows = dataset?.data?.rows?.length || dataset?.data?.data?.features?.length || 0; return `
${id} ${rows.toLocaleString()} rows
`; }).join(''); container.innerHTML = `

KeplerGL Map

Interactive geospatial visualization

${datasetCount}
Datasets
${formatBytes(totalDataSize)}
Data Size
${readOnly ? 'Read Only' : 'Interactive'}
Mode

Map View

Center [${center[0].toFixed(4)}, ${center[1].toFixed(4)}]
Zoom ${zoom}
${datasetCount > 0 ? `

Datasets

${datasetItems}
` : `

No datasets loaded. Use add_data() to add data.

`}

Use to_html() to export as standalone interactive map

`; return container; } /** * anywidget render function. */ function render({ model, el }: { model: KeplerGLModel; el: HTMLElement }): () => void { el.style.width = '100%'; el.style.display = 'block'; // Create container const container = document.createElement('div'); container.style.width = model.get('width') || '100%'; container.style.height = model.get('height') || 'auto'; container.style.minHeight = '300px'; container.style.position = 'relative'; el.appendChild(container); // Create widget content const content = createWidgetContent(model); container.appendChild(content); // Handle model changes const updateContent = () => { container.innerHTML = ''; const newContent = createWidgetContent(model); container.appendChild(newContent); }; model.on('change:datasets', updateContent); model.on('change:config', updateContent); model.on('change:center', updateContent); model.on('change:zoom', updateContent); // Return cleanup function return () => { model.off('change:datasets', updateContent); model.off('change:config', updateContent); model.off('change:center', updateContent); model.off('change:zoom', updateContent); if (container.parentNode) { container.parentNode.removeChild(container); } }; } export default { render };