pandrs 0.3.0

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
//! Jupyter Notebook Integration for PandRS
//!
//! This module provides enhanced integration with Jupyter notebooks including:
//! - Rich HTML display for DataFrames and Series
//! - Interactive widgets and visualizations
//! - Custom display formatting and styling
//! - Jupyter magic commands support
//! - Better integration with Jupyter's display system

use lazy_static::lazy_static;
use std::collections::HashMap;
use std::fmt::Write;
use std::sync::RwLock;

use crate::core::error::{Error, Result};
use crate::dataframe::base::DataFrame;
use crate::series::base::Series;
use crate::{read_lock_safe, write_lock_safe};

/// Jupyter display configuration
#[derive(Debug, Clone)]
pub struct JupyterConfig {
    /// Maximum number of rows to display
    pub max_rows: usize,
    /// Maximum number of columns to display
    pub max_columns: usize,
    /// Enable interactive features
    pub interactive: bool,
    /// Display precision for floating point numbers
    pub precision: usize,
    /// Color scheme for styling
    pub color_scheme: JupyterColorScheme,
    /// Table styling options
    pub table_style: TableStyle,
    /// Enable syntax highlighting
    pub syntax_highlighting: bool,
}

impl Default for JupyterConfig {
    fn default() -> Self {
        Self {
            max_rows: 100,
            max_columns: 20,
            interactive: true,
            precision: 6,
            color_scheme: JupyterColorScheme::Default,
            table_style: TableStyle::default(),
            syntax_highlighting: true,
        }
    }
}

/// Color schemes for Jupyter display
#[derive(Debug, Clone)]
pub enum JupyterColorScheme {
    /// Default color scheme
    Default,
    /// Dark theme
    Dark,
    /// Light theme  
    Light,
    /// Custom color scheme
    Custom {
        header_bg: String,
        header_text: String,
        row_bg: String,
        alt_row_bg: String,
        text_color: String,
        border_color: String,
    },
}

impl JupyterColorScheme {
    /// Get CSS styles for the color scheme
    pub fn to_css(&self) -> String {
        match self {
            JupyterColorScheme::Default => r#"
                .pandrs-table { background-color: #ffffff; color: #333333; }
                .pandrs-header { background-color: #f8f9fa; color: #495057; font-weight: bold; }
                .pandrs-row { background-color: #ffffff; }
                .pandrs-row:nth-child(even) { background-color: #f8f9fa; }
                .pandrs-border { border-color: #dee2e6; }
            "#
            .to_string(),

            JupyterColorScheme::Dark => r#"
                .pandrs-table { background-color: #2d3748; color: #e2e8f0; }
                .pandrs-header { background-color: #4a5568; color: #f7fafc; font-weight: bold; }
                .pandrs-row { background-color: #2d3748; }
                .pandrs-row:nth-child(even) { background-color: #4a5568; }
                .pandrs-border { border-color: #718096; }
            "#
            .to_string(),

            JupyterColorScheme::Light => r#"
                .pandrs-table { background-color: #fefefe; color: #2d3748; }
                .pandrs-header { background-color: #edf2f7; color: #2d3748; font-weight: bold; }
                .pandrs-row { background-color: #fefefe; }
                .pandrs-row:nth-child(even) { background-color: #f7fafc; }
                .pandrs-border { border-color: #cbd5e0; }
            "#
            .to_string(),

            JupyterColorScheme::Custom {
                header_bg,
                header_text,
                row_bg,
                alt_row_bg,
                text_color,
                border_color,
            } => {
                format!(
                    r#"
                    .pandrs-table {{ background-color: {}; color: {}; }}
                    .pandrs-header {{ background-color: {}; color: {}; font-weight: bold; }}
                    .pandrs-row {{ background-color: {}; }}
                    .pandrs-row:nth-child(even) {{ background-color: {}; }}
                    .pandrs-border {{ border-color: {}; }}
                "#,
                    row_bg, text_color, header_bg, header_text, row_bg, alt_row_bg, border_color
                )
            }
        }
    }
}

/// Table styling options
#[derive(Debug, Clone)]
pub struct TableStyle {
    /// Show borders around cells
    pub show_borders: bool,
    /// Show row numbers/index
    pub show_index: bool,
    /// Show column data types
    pub show_dtypes: bool,
    /// Show summary statistics
    pub show_summary: bool,
    /// Table width setting
    pub width: TableWidth,
    /// Cell padding
    pub cell_padding: String,
    /// Font family
    pub font_family: String,
    /// Font size
    pub font_size: String,
}

impl Default for TableStyle {
    fn default() -> Self {
        Self {
            show_borders: true,
            show_index: true,
            show_dtypes: false,
            show_summary: false,
            width: TableWidth::Auto,
            cell_padding: "8px 12px".to_string(),
            font_family: "'Monaco', 'Menlo', 'Ubuntu Mono', monospace".to_string(),
            font_size: "13px".to_string(),
        }
    }
}

/// Table width options
#[derive(Debug, Clone)]
pub enum TableWidth {
    /// Automatic width
    Auto,
    /// Fixed width in pixels
    Fixed(u32),
    /// Percentage of container
    Percentage(u32),
    /// Full width
    Full,
}

impl TableWidth {
    fn to_css(&self) -> String {
        match self {
            TableWidth::Auto => "width: auto;".to_string(),
            TableWidth::Fixed(px) => format!("width: {}px;", px),
            TableWidth::Percentage(pct) => format!("width: {}%;", pct),
            TableWidth::Full => "width: 100%;".to_string(),
        }
    }
}

/// Jupyter display trait for DataFrames
pub trait JupyterDisplay {
    /// Generate rich HTML display for Jupyter
    fn to_jupyter_html(&self, config: &JupyterConfig) -> Result<String>;

    /// Generate interactive widget HTML
    fn to_interactive_widget(&self, config: &JupyterConfig) -> Result<String>;

    /// Generate summary display
    fn to_summary_html(&self, config: &JupyterConfig) -> Result<String>;

    /// Generate data explorer widget
    fn to_data_explorer(&self, config: &JupyterConfig) -> Result<String>;
}

impl JupyterDisplay for DataFrame {
    fn to_jupyter_html(&self, config: &JupyterConfig) -> Result<String> {
        let mut html = String::new();

        // CSS styles
        html.push_str(&format!(
            r#"
        <style>
        .pandrs-container {{
            margin: 10px 0;
            font-family: {};
            font-size: {};
        }}
        .pandrs-table {{
            border-collapse: collapse;
            margin: 10px 0;
            {}
        }}
        .pandrs-table th, .pandrs-table td {{
            padding: {};
            text-align: left;
            border: {};
        }}
        {}
        .pandrs-info {{
            margin: 5px 0;
            font-size: 12px;
            color: #666;
        }}
        .pandrs-dtype {{
            font-style: italic;
            color: #888;
            font-size: 11px;
        }}
        </style>
        "#,
            config.table_style.font_family,
            config.table_style.font_size,
            config.table_style.width.to_css(),
            config.table_style.cell_padding,
            if config.table_style.show_borders {
                "1px solid var(--pandrs-border-color, #ddd)"
            } else {
                "none"
            },
            config.color_scheme.to_css()
        ));

        // Container start
        html.push_str(r#"<div class="pandrs-container">"#);

        // DataFrame info
        html.push_str(&format!(
            r#"<div class="pandrs-info">DataFrame: {} rows × {} columns</div>"#,
            self.row_count(),
            self.column_names().len()
        ));

        // Table start
        html.push_str(r#"<table class="pandrs-table">"#);

        // Header
        html.push_str("<thead><tr class='pandrs-header'>");

        if config.table_style.show_index {
            html.push_str("<th></th>"); // Index column header
        }

        let columns = self.column_names();
        let visible_columns = if columns.len() > config.max_columns {
            &columns[..config.max_columns]
        } else {
            &columns
        };

        for col_name in visible_columns {
            html.push_str(&format!("<th>{}</th>", escape_html(col_name)));
        }

        if columns.len() > config.max_columns {
            html.push_str("<th>...</th>");
        }

        html.push_str("</tr>");

        // Column types (if enabled)
        if config.table_style.show_dtypes {
            html.push_str("<tr class='pandrs-header'>");
            if config.table_style.show_index {
                html.push_str("<td class='pandrs-dtype'></td>");
            }
            for col_name in visible_columns {
                // Try to infer column type
                let dtype = self.infer_column_type(col_name);
                html.push_str(&format!("<td class='pandrs-dtype'>{}</td>", dtype));
            }
            if columns.len() > config.max_columns {
                html.push_str("<td class='pandrs-dtype'>...</td>");
            }
            html.push_str("</tr>");
        }

        html.push_str("</thead>");

        // Body
        html.push_str("<tbody>");

        let row_count = self.row_count();
        let visible_rows = if row_count > config.max_rows {
            config.max_rows / 2
        } else {
            row_count
        };

        // Display first rows
        for i in 0..visible_rows.min(row_count) {
            html.push_str(&format!("<tr class='pandrs-row'>"));

            if config.table_style.show_index {
                html.push_str(&format!("<td><strong>{}</strong></td>", i));
            }

            for col_name in visible_columns {
                let value = self
                    .get_value_at(i, col_name)
                    .unwrap_or_else(|_| "NaN".to_string());
                let formatted_value = format_value(&value, config.precision);
                html.push_str(&format!("<td>{}</td>", escape_html(&formatted_value)));
            }

            if columns.len() > config.max_columns {
                html.push_str("<td>...</td>");
            }

            html.push_str("</tr>");
        }

        // Show ellipsis if there are more rows
        if row_count > config.max_rows {
            html.push_str("<tr class='pandrs-row'>");
            if config.table_style.show_index {
                html.push_str("<td>...</td>");
            }
            for _ in 0..visible_columns.len() {
                html.push_str("<td>...</td>");
            }
            if columns.len() > config.max_columns {
                html.push_str("<td>...</td>");
            }
            html.push_str("</tr>");

            // Display last rows
            let remaining_rows = config.max_rows - visible_rows;
            let start_idx = row_count - remaining_rows;

            for i in start_idx..row_count {
                html.push_str(&format!("<tr class='pandrs-row'>"));

                if config.table_style.show_index {
                    html.push_str(&format!("<td><strong>{}</strong></td>", i));
                }

                for col_name in visible_columns {
                    let value = self
                        .get_value_at(i, col_name)
                        .unwrap_or_else(|_| "NaN".to_string());
                    let formatted_value = format_value(&value, config.precision);
                    html.push_str(&format!("<td>{}</td>", escape_html(&formatted_value)));
                }

                if columns.len() > config.max_columns {
                    html.push_str("<td>...</td>");
                }

                html.push_str("</tr>");
            }
        }

        html.push_str("</tbody>");
        html.push_str("</table>");

        // Summary info (if enabled)
        if config.table_style.show_summary {
            html.push_str(&format!(
                r#"<div class="pandrs-info">
                Memory usage: ~{} KB | 
                Columns: {} | 
                Data types: {}
                </div>"#,
                estimate_memory_usage(self),
                columns.len(),
                self.get_column_types().join(", ")
            ));
        }

        html.push_str("</div>");

        Ok(html)
    }

    fn to_interactive_widget(&self, config: &JupyterConfig) -> Result<String> {
        let mut html = String::new();

        // Interactive widget with JavaScript
        html.push_str(&format!(
            r#"
        <div id="pandrs-widget-{}" class="pandrs-interactive-widget">
            <style>
            .pandrs-interactive-widget {{
                border: 1px solid #ddd;
                border-radius: 5px;
                padding: 10px;
                margin: 10px 0;
                font-family: {};
            }}
            .pandrs-controls {{
                margin-bottom: 10px;
                padding: 5px;
                background-color: #f8f9fa;
                border-radius: 3px;
            }}
            .pandrs-controls button {{
                margin: 2px;
                padding: 5px 10px;
                border: 1px solid #ccc;
                background-color: #fff;
                cursor: pointer;
                border-radius: 3px;
            }}
            .pandrs-controls button:hover {{
                background-color: #e9ecef;
            }}
            .pandrs-search {{
                padding: 5px;
                border: 1px solid #ccc;
                border-radius: 3px;
                margin: 0 5px;
            }}
            </style>
            
            <div class="pandrs-controls">
                <button onclick="pandrs_show_info('{}')">📊 Info</button>
                <button onclick="pandrs_show_describe('{}')">📈 Describe</button>
                <button onclick="pandrs_show_plot('{}')">📊 Plot</button>
                <input type="text" class="pandrs-search" placeholder="Search columns..." 
                       onkeyup="pandrs_filter_columns('{}', this.value)">
            </div>
            
            <div id="pandrs-content-{}">
                {}
            </div>
        </div>
        
        <script>
        function pandrs_show_info(widget_id) {{
            document.getElementById('pandrs-content-' + widget_id).innerHTML = `
                <h4>DataFrame Information</h4>
                <ul>
                    <li>Rows: {}</li>
                    <li>Columns: {}</li>
                    <li>Memory usage: ~{} KB</li>
                    <li>Column types: {}</li>
                </ul>
            `;
        }}
        
        function pandrs_show_describe(widget_id) {{
            document.getElementById('pandrs-content-' + widget_id).innerHTML = `
                <h4>Statistical Description</h4>
                <p>Summary statistics would be displayed here...</p>
            `;
        }}
        
        function pandrs_show_plot(widget_id) {{
            document.getElementById('pandrs-content-' + widget_id).innerHTML = `
                <h4>Quick Plot</h4>
                <p>Interactive plotting widget would be displayed here...</p>
            `;
        }}
        
        function pandrs_filter_columns(widget_id, filter) {{
            // Column filtering functionality would be implemented here
            console.log('Filtering columns by:', filter);
        }}
        </script>
        "#,
            self.row_count(), // widget id
            config.table_style.font_family,
            self.row_count(), // info button
            self.row_count(), // describe button
            self.row_count(), // plot button
            self.row_count(), // search input
            self.row_count(), // content div
            self.to_jupyter_html(config)?,
            self.row_count(),
            self.column_names().len(),
            estimate_memory_usage(self),
            self.get_column_types().join(", ")
        ));

        Ok(html)
    }

    fn to_summary_html(&self, config: &JupyterConfig) -> Result<String> {
        let mut html = String::new();

        html.push_str(&format!(
            r#"
        <div class="pandrs-summary" style="
            font-family: {};
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin: 10px 0;
            background-color: #f8f9fa;
        ">
            <h3 style="margin-top: 0;">DataFrame Summary</h3>
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                <div>
                    <h4>Basic Information</h4>
                    <ul style="list-style: none; padding: 0;">
                        <li><strong>Shape:</strong> {} rows × {} columns</li>
                        <li><strong>Memory usage:</strong> ~{} KB</li>
                        <li><strong>Non-null values:</strong> {}</li>
                        <li><strong>Null values:</strong> {}</li>
                    </ul>
                </div>
                <div>
                    <h4>Column Information</h4>
                    <ul style="list-style: none; padding: 0; max-height: 200px; overflow-y: auto;">
        "#,
            config.table_style.font_family,
            self.row_count(),
            self.column_names().len(),
            estimate_memory_usage(self),
            estimate_non_null_count(self),
            estimate_null_count(self)
        ));

        // Column details
        for (i, col_name) in self.column_names().iter().enumerate() {
            let col_type = self.infer_column_type(col_name);
            html.push_str(&format!(
                "<li><strong>{}:</strong> {} <span style='color: #666; font-size: 0.9em'>({})</span></li>",
                escape_html(col_name),
                col_type,
                format!("col {}", i)
            ));
        }

        html.push_str(
            r#"
                    </ul>
                </div>
            </div>
        </div>
        "#,
        );

        Ok(html)
    }

    fn to_data_explorer(&self, config: &JupyterConfig) -> Result<String> {
        let widget_id = format!("explorer_{}", self.row_count());

        let mut html = String::new();

        html.push_str(&format!(r#"
        <div id="pandrs-explorer-{}" class="pandrs-data-explorer">
            <style>
            .pandrs-data-explorer {{
                border: 1px solid #ddd;
                border-radius: 8px;
                padding: 0;
                margin: 15px 0;
                font-family: {};
                background-color: #fff;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }}
            .pandrs-explorer-header {{
                background-color: #007bff;
                color: white;
                padding: 12px 15px;
                border-radius: 8px 8px 0 0;
                font-weight: bold;
            }}
            .pandrs-explorer-tabs {{
                display: flex;
                background-color: #f8f9fa;
                border-bottom: 1px solid #ddd;
            }}
            .pandrs-explorer-tab {{
                padding: 10px 20px;
                cursor: pointer;
                border: none;
                background: none;
                border-bottom: 3px solid transparent;
                font-weight: 500;
            }}
            .pandrs-explorer-tab.active {{
                background-color: white;
                border-bottom-color: #007bff;
                color: #007bff;
            }}
            .pandrs-explorer-content {{
                padding: 15px;
                min-height: 300px;
            }}
            </style>
            
            <div class="pandrs-explorer-header">
                🔍 DataFrame Explorer - {} rows × {} columns
            </div>
            
            <div class="pandrs-explorer-tabs">
                <button class="pandrs-explorer-tab active" onclick="pandrs_show_tab('{}', 'data')">
                    📊 Data
                </button>
                <button class="pandrs-explorer-tab" onclick="pandrs_show_tab('{}', 'info')">
                    ℹ️ Info
                </button>
                <button class="pandrs-explorer-tab" onclick="pandrs_show_tab('{}', 'stats')">
                    📈 Statistics
                </button>
                <button class="pandrs-explorer-tab" onclick="pandrs_show_tab('{}', 'viz')">
                    📊 Visualize
                </button>
            </div>
            
            <div class="pandrs-explorer-content">
                <div id="pandrs-tab-data-{}" class="pandrs-tab-content">
                    {}
                </div>
                <div id="pandrs-tab-info-{}" class="pandrs-tab-content" style="display: none;">
                    {}
                </div>
                <div id="pandrs-tab-stats-{}" class="pandrs-tab-content" style="display: none;">
                    <h4>Statistical Summary</h4>
                    <p>Descriptive statistics would be computed and displayed here...</p>
                </div>
                <div id="pandrs-tab-viz-{}" class="pandrs-tab-content" style="display: none;">
                    <h4>Quick Visualizations</h4>
                    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px;">
                        <button onclick="alert('Histogram plot would be generated')">📊 Histogram</button>
                        <button onclick="alert('Scatter plot would be generated')">🔸 Scatter Plot</button>
                        <button onclick="alert('Box plot would be generated')">📦 Box Plot</button>
                        <button onclick="alert('Correlation matrix would be generated')">🔗 Correlation</button>
                    </div>
                </div>
            </div>
        </div>
        
        <script>
        function pandrs_show_tab(explorer_id, tab_name) {{
            // Hide all tabs
            ['data', 'info', 'stats', 'viz'].forEach(function(name) {{
                var element = document.getElementById('pandrs-tab-' + name + '-' + explorer_id);
                if (element) element.style.display = 'none';
            }});
            
            // Remove active class from all tab buttons
            var tabs = document.querySelectorAll('#pandrs-explorer-' + explorer_id + ' .pandrs-explorer-tab');
            tabs.forEach(function(tab) {{
                tab.classList.remove('active');
            }});
            
            // Show selected tab
            var selectedTab = document.getElementById('pandrs-tab-' + tab_name + '-' + explorer_id);
            if (selectedTab) selectedTab.style.display = 'block';
            
            // Add active class to clicked button
            event.target.classList.add('active');
        }}
        </script>
        "#,
            widget_id,
            config.table_style.font_family,
            self.row_count(),
            self.column_names().len(),
            widget_id, // data tab
            widget_id, // info tab
            widget_id, // stats tab
            widget_id, // viz tab
            widget_id, // data content div
            self.to_jupyter_html(config)?,
            widget_id, // info content div
            self.to_summary_html(config)?,
            widget_id, // stats content div
            widget_id  // viz content div
        ));

        Ok(html)
    }
}

impl<T> JupyterDisplay for Series<T>
where
    T: Clone + std::fmt::Display + std::fmt::Debug,
{
    fn to_jupyter_html(&self, config: &JupyterConfig) -> Result<String> {
        let mut html = String::new();

        // CSS styles
        html.push_str(&format!(
            r#"
        <style>
        .pandrs-series-container {{
            margin: 10px 0;
            font-family: {};
            font-size: {};
        }}
        .pandrs-series-table {{
            border-collapse: collapse;
            margin: 10px 0;
            {}
        }}
        .pandrs-series-table th, .pandrs-series-table td {{
            padding: {};
            text-align: left;
            border: {};
        }}
        {}
        .pandrs-series-info {{
            margin: 5px 0;
            font-size: 12px;
            color: #666;
        }}
        </style>
        "#,
            config.table_style.font_family,
            config.table_style.font_size,
            config.table_style.width.to_css(),
            config.table_style.cell_padding,
            if config.table_style.show_borders {
                "1px solid var(--pandrs-border-color, #ddd)"
            } else {
                "none"
            },
            config.color_scheme.to_css()
        ));

        // Container start
        html.push_str(r#"<div class="pandrs-series-container">"#);

        // Series info
        let series_name = self.name().map_or("Unnamed", |s| s.as_str());
        html.push_str(&format!(
            r#"<div class="pandrs-series-info">Series '{}': {} values</div>"#,
            escape_html(series_name),
            self.len()
        ));

        // Table start
        html.push_str(r#"<table class="pandrs-series-table">"#);

        // Header
        html.push_str("<thead><tr class='pandrs-header'>");
        if config.table_style.show_index {
            html.push_str("<th>Index</th>");
        }
        html.push_str(&format!("<th>{}</th>", escape_html(series_name)));
        html.push_str("</tr></thead>");

        // Body
        html.push_str("<tbody>");

        let values = self.values();
        let visible_count = if values.len() > config.max_rows {
            config.max_rows / 2
        } else {
            values.len()
        };

        // Display first values
        for (i, value) in values.iter().enumerate().take(visible_count) {
            html.push_str(&format!("<tr class='pandrs-row'>"));

            if config.table_style.show_index {
                html.push_str(&format!("<td><strong>{}</strong></td>", i));
            }

            let formatted_value = format!("{}", value);
            html.push_str(&format!("<td>{}</td>", escape_html(&formatted_value)));
            html.push_str("</tr>");
        }

        // Show ellipsis if there are more values
        if values.len() > config.max_rows {
            html.push_str("<tr class='pandrs-row'>");
            if config.table_style.show_index {
                html.push_str("<td>...</td>");
            }
            html.push_str("<td>...</td>");
            html.push_str("</tr>");

            // Display last values
            let remaining_count = config.max_rows - visible_count;
            let start_idx = values.len() - remaining_count;

            for (i, value) in values.iter().enumerate().skip(start_idx) {
                html.push_str(&format!("<tr class='pandrs-row'>"));

                if config.table_style.show_index {
                    html.push_str(&format!("<td><strong>{}</strong></td>", i));
                }

                let formatted_value = format!("{}", value);
                html.push_str(&format!("<td>{}</td>", escape_html(&formatted_value)));
                html.push_str("</tr>");
            }
        }

        html.push_str("</tbody>");
        html.push_str("</table>");
        html.push_str("</div>");

        Ok(html)
    }

    fn to_interactive_widget(&self, config: &JupyterConfig) -> Result<String> {
        // Simplified interactive widget for Series
        let mut html = String::new();
        let series_name = self.name().map_or("Unnamed", |s| s.as_str());

        html.push_str(&format!(
            r#"
        <div class="pandrs-series-widget">
            <div style="margin-bottom: 10px; font-weight: bold;">
                Series '{}' Interactive Widget
            </div>
            {}
        </div>
        "#,
            escape_html(series_name),
            self.to_jupyter_html(config)?
        ));

        Ok(html)
    }

    fn to_summary_html(&self, config: &JupyterConfig) -> Result<String> {
        let series_name = self.name().map_or("Unnamed", |s| s.as_str());

        Ok(format!(
            r#"
        <div class="pandrs-series-summary" style="
            font-family: {};
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin: 10px 0;
            background-color: #f8f9fa;
        ">
            <h3 style="margin-top: 0;">Series '{}' Summary</h3>
            <ul style="list-style: none; padding: 0;">
                <li><strong>Length:</strong> {} values</li>
                <li><strong>Non-null values:</strong> {}</li>
                <li><strong>Data type:</strong> {}</li>
            </ul>
        </div>
        "#,
            config.table_style.font_family,
            escape_html(series_name),
            self.len(),
            self.len(), // Assume all non-null for now
            std::any::type_name::<T>()
        ))
    }

    fn to_data_explorer(&self, config: &JupyterConfig) -> Result<String> {
        // Use the summary for series data explorer
        self.to_summary_html(config)
    }
}

/// Magic commands support for Jupyter
pub struct JupyterMagics;

impl JupyterMagics {
    /// Register PandRS magic commands
    pub fn register_magics() -> String {
        r#"
        # PandRS Magic Commands for Jupyter
        
        from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
        from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
        
        @magics_class
        class PandRSMagics(Magics):
            
            @line_magic
            def pandrs_info(self, line):
                """Display PandRS version and configuration info"""
                return "PandRS 0.3.0 - High-performance DataFrame library for Rust"
            
            @line_magic 
            def pandrs_config(self, line):
                """Configure PandRS display settings for Jupyter"""
                # Configuration management would be implemented here
                return "PandRS configuration updated"
            
            @cell_magic
            def pandrs_sql(self, line, cell):
                """Execute SQL-like queries on DataFrames"""
                # SQL query execution would be implemented here
                return f"SQL query executed: {cell}"
        
        # Register the magic class
        ip = get_ipython()
        ip.register_magic_functions(PandRSMagics)
        "#
        .to_string()
    }
}

// Helper functions

fn escape_html(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
}

fn format_value(value: &str, precision: usize) -> String {
    // Try to parse as float for better formatting
    if let Ok(f) = value.parse::<f64>() {
        if f.fract() == 0.0 && f.abs() < 1e10 {
            format!("{:.0}", f)
        } else {
            format!("{:.prec$}", f, prec = precision)
        }
    } else {
        value.to_string()
    }
}

fn estimate_memory_usage(df: &DataFrame) -> u64 {
    // Rough estimate: number of cells * average cell size
    let cell_count = df.row_count() * df.column_names().len();
    (cell_count * 8) as u64 / 1024 // Convert to KB
}

fn estimate_non_null_count(df: &DataFrame) -> usize {
    // Simplified: assume all values are non-null for now
    df.row_count() * df.column_names().len()
}

fn estimate_null_count(df: &DataFrame) -> usize {
    // Simplified: assume no null values for now
    0
}

impl DataFrame {
    /// Infer the type of a column for display purposes
    fn infer_column_type(&self, column_name: &str) -> String {
        // Try to get a sample value and infer type
        if let Ok(value) = self.get_value_at(0, column_name) {
            if value.parse::<i64>().is_ok() {
                "integer".to_string()
            } else if value.parse::<f64>().is_ok() {
                "float".to_string()
            } else if value.parse::<bool>().is_ok() {
                "boolean".to_string()
            } else {
                "string".to_string()
            }
        } else {
            "unknown".to_string()
        }
    }

    /// Get column types for all columns
    fn get_column_types(&self) -> Vec<String> {
        self.column_names()
            .iter()
            .map(|col| self.infer_column_type(col))
            .collect()
    }

    /// Get value at specific position (helper method)
    fn get_value_at(&self, row: usize, column: &str) -> Result<String> {
        let values = self.get_column_string_values(column)?;
        if row < values.len() {
            Ok(values[row].clone())
        } else {
            Err(Error::IndexOutOfBounds {
                index: row,
                size: values.len(),
            })
        }
    }
}

lazy_static! {
    /// Display configuration for Jupyter notebooks
    static ref JUPYTER_CONFIG: RwLock<Option<JupyterConfig>> = RwLock::new(None);
}

/// Get the current Jupyter configuration
pub fn get_jupyter_config() -> JupyterConfig {
    read_lock_safe!(JUPYTER_CONFIG, "jupyter config read")
        .ok()
        .and_then(|c| c.clone())
        .unwrap_or_default()
}

/// Set the Jupyter configuration
pub fn set_jupyter_config(config: JupyterConfig) {
    if let Ok(mut cfg) = write_lock_safe!(JUPYTER_CONFIG, "jupyter config write") {
        *cfg = Some(config);
    }
}

/// Initialize Jupyter integration with default settings
pub fn init_jupyter() -> Result<()> {
    set_jupyter_config(JupyterConfig::default());

    // Print initialization message
    println!("🎯 PandRS Jupyter Integration Initialized!");
    println!("📚 Enhanced DataFrame display and interactive widgets are now available.");
    println!("🔍 Use .to_jupyter_html(), .to_interactive_widget(), or .to_data_explorer() on DataFrames.");

    Ok(())
}

/// Create a quick configuration for dark mode
pub fn jupyter_dark_mode() -> JupyterConfig {
    JupyterConfig {
        color_scheme: JupyterColorScheme::Dark,
        ..Default::default()
    }
}

/// Create a quick configuration for light mode  
pub fn jupyter_light_mode() -> JupyterConfig {
    JupyterConfig {
        color_scheme: JupyterColorScheme::Light,
        ..Default::default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_jupyter_config_default() {
        let config = JupyterConfig::default();
        assert_eq!(config.max_rows, 100);
        assert_eq!(config.max_columns, 20);
        assert!(config.interactive);
    }

    #[test]
    fn test_color_scheme_css() {
        let dark_scheme = JupyterColorScheme::Dark;
        let css = dark_scheme.to_css();
        assert!(css.contains("background-color: #2d3748"));
    }

    #[test]
    fn test_table_width_css() {
        let auto_width = TableWidth::Auto;
        assert_eq!(auto_width.to_css(), "width: auto;");

        let fixed_width = TableWidth::Fixed(800);
        assert_eq!(fixed_width.to_css(), "width: 800px;");
    }

    #[test]
    fn test_escape_html() {
        assert_eq!(escape_html("<test>"), "&lt;test&gt;");
        assert_eq!(escape_html("AT&T"), "AT&amp;T");
    }

    #[test]
    fn test_format_value() {
        assert_eq!(format_value("3.14159", 2), "3.14");
        assert_eq!(format_value("42", 2), "42");
        assert_eq!(format_value("hello", 2), "hello");
    }

    #[test]
    fn test_jupyter_html_generation() {
        let mut data = HashMap::new();
        data.insert("col1".to_string(), vec!["1".to_string(), "2".to_string()]);
        data.insert("col2".to_string(), vec!["a".to_string(), "b".to_string()]);

        let df = DataFrame::from_map(data, None).expect("operation should succeed");
        let config = JupyterConfig::default();

        let html = df
            .to_jupyter_html(&config)
            .expect("operation should succeed");
        assert!(html.contains("pandrs-table"));
        assert!(html.contains("col1"));
        assert!(html.contains("col2"));
    }
}