Browse Source

started work on adding multi-dataset capability to DataEditor

master
phryk 1 month ago
parent
commit
68d16d2615
2 changed files with 82 additions and 40 deletions
  1. 66
    29
      poobrains/svg/plot.py
  2. 16
    11
      poobrains/themes/default/form/dataeditor.jinja

+ 66
- 29
poobrains/svg/plot.py View File

@@ -421,7 +421,7 @@ class Plot(base.SVG):
421 421
 
422 422
     def dataset_griddable(self, dataset):
423 423
 
424
-        app.debugger.set_trace()
424
+        #app.debugger.set_trace()
425 425
         valid_dtypes = [
426 426
            int,
427 427
            float
@@ -511,7 +511,7 @@ class Plot(base.SVG):
511 511
     @locked_cached_property
512 512
     def grid_x(self):
513 513
 
514
-        app.debugger.set_trace()
514
+        #app.debugger.set_trace()
515 515
         if self.span_x == 0:
516 516
             return [self.min_x]
517 517
 
@@ -650,12 +650,9 @@ class EditorNewSessionFieldset(poobrains.form.Fieldset):
650 650
 
651 651
         session['editor-sessions'][handle] = {
652 652
             'handle': handle,
653
-            'plot_kind': 'scatter',
654 653
             'action': None,
655
-            'action_data': None,
656
-            'title': None,
657
-            'description': None,
658
-            'data': None,
654
+            'action_data': {},
655
+            'datasets': {},
659 656
         }
660 657
 
661 658
         return redirect(DataEditor.url(handle=handle, mode='full'))
@@ -663,7 +660,12 @@ class EditorNewSessionFieldset(poobrains.form.Fieldset):
663 660
 
664 661
 class EditorLoadFieldset(poobrains.form.Fieldset):
665 662
 
663
+    title = "Add new dataset to session"
664
+
666 665
     dataset = poobrains.form.fields.Select(choices=dataset_choices)
666
+    dataset_name = poobrains.form.fields.Text(label='Dataset name')
667
+    dataset_title = poobrains.form.fields.Text(label='Dataset title')
668
+    dataset_description = poobrains.form.fields.TextArea(label='Dataset description')
667 669
     load_dataset = poobrains.form.Button(label='Load', type='submit')
668 670
 
669 671
 
@@ -674,14 +676,20 @@ class EditorLoadFieldset(poobrains.form.Fieldset):
674 676
         super(EditorLoadFieldset, self).__init__(**kwargs)
675 677
 
676 678
         self.editor_handle = editor_handle
677
-        action = session['editor-sessions'][self.editor_handle]['action']
679
+        editor_session = session['editor-sessions'][self.editor_handle]
678 680
 
679
-        if action.startswith('load.dynamic.'):
681
+        if editor_session['action'].startswith('load.dynamic.'):
680 682
 
681
-            dataset_name = action.split('.')[2]
683
+            dataset_name = editor_session['action'].split('.')[2]
682 684
             self.fields['dataset'].value = '_%s' % dataset_name
683 685
             self.fields['dataset'].readonly = True
684 686
 
687
+            self.fields['dataset_name'].value = editor_session['action_data']['dataset_name']
688
+            self.fields['dataset_name'].readonly = True
689
+
690
+            self.fields['dataset_description'].value = editor_session['action_data']['dataset_description']
691
+            self.fields['dataset_description'].readonly = True
692
+
685 693
             if dataset_name in datasets:
686 694
 
687 695
                 dataset_class = datasets[dataset_name]
@@ -714,6 +722,11 @@ class EditorLoadFieldset(poobrains.form.Fieldset):
714 722
     
715 723
             if submit == '%s.load_dataset' % self.name:
716 724
 
725
+                session['editor-sessions'][self.editor_handle]['action_data'] = {
726
+                    'dataset_name': self.fields['dataset_name'].value,
727
+                    'dataset_description': self.fields['dataset_description'].value
728
+                }
729
+
717 730
                 if dataset_name.startswith('_'):
718 731
 
719 732
                     dataset_name = dataset_name[1:]
@@ -723,16 +736,18 @@ class EditorLoadFieldset(poobrains.form.Fieldset):
723 736
                         session['editor-sessions'][instance.handle_string]['action'] = 'load.dynamic.%s' % dataset_name
724 737
                         self.summon_parameter_fieldset(datasets[dataset_name])
725 738
                         self.fields['dataset'].readonly = True
739
+                        self.fields['dataset_name'].readonly = True
726 740
                 else: # directly loadable dataset
727 741
 
728 742
                     try:
729 743
                         dataset = StoredDataset.load(dataset_name)
730 744
                         dataset.permissions['read'].check(g.user)
731
-                        instance.dataset = dataset
745
+                        dataset.name = self.fields['dataset_name'].value
746
+                        instance.datasets[dataset.name] = dataset
732 747
                         session['editor-sessions'][instance.handle_string]['action'] = None
733
-                        session['editor-sessions'][instance.handle_string]['title'] = instance.dataset.title
734
-                        session['editor-sessions'][instance.handle_string]['description'] = instance.dataset.description
735
-                        session['editor-sessions'][instance.handle_string]['data'] = instance.dataset.data.to_dict()
748
+                        session['editor-sessions'][instance.handle_string]['datasets'][dataset.name]['title'] = dataset.title
749
+                        session['editor-sessions'][instance.handle_string]['datasets'][dataset.name]['description'] = dataset.description
750
+                        session['editor-sessions'][instance.handle_string]['datasets'][dataset.name]['data'] = dataset.data.to_dict()
736 751
                     except StoredDataset.DoesNotExist:
737 752
                         flash('Unknown stored dataset: %s' % dataset_name, 'error')
738 753
 
@@ -758,13 +773,19 @@ class EditorLoadFieldset(poobrains.form.Fieldset):
758 773
 
759 774
                     dataset = dataset_class()
760 775
                     dataset.fill(**param_values)
776
+                    dataset.name = self.fields['dataset_name'].value
777
+                    dataset.title = self.fields['dataset_title'].value
778
+                    dataset.description = self.fields['dataset_description'].value
761 779
 
762 780
                     dataset.permissions['read'].check(g.user)
763
-                    instance.dataset = dataset
781
+                    instance.datasets[dataset.name] = dataset
764 782
                     session['editor-sessions'][instance.handle_string]['action'] = None
765
-                    session['editor-sessions'][instance.handle_string]['title'] = instance.dataset.title
766
-                    session['editor-sessions'][instance.handle_string]['description'] = instance.dataset.description
767
-                    session['editor-sessions'][instance.handle_string]['data'] = instance.dataset.data.to_dict()
783
+                    session['editor-sessions'][instance.handle_string]['datasets'][dataset.name] = {
784
+                        'title': dataset.title,
785
+                        'description': dataset.description,
786
+                        'data': dataset.data.to_dict(),
787
+                        'plot_kind': 'scatter',
788
+                    }
768 789
                     del(self.fields['dynamic'])
769 790
                     self.fields['dataset'].readonly = False
770 791
 
@@ -780,7 +801,9 @@ class PlotKindFieldset(poobrains.form.Fieldset):
780 801
         self.editor_handle = editor_handle
781 802
         action = session['editor-sessions'][self.editor_handle]['action']
782 803
 
783
-        self.kind = poobrains.form.fields.Select(choices=[(kind, cls.__name__) for (kind, cls) in plot_kinds.items()], default=session['editor-sessions'][editor_handle]['plot_kind'])
804
+        k = next(iter(session['editor-sessions'][self.editor_handle]['datasets']))
805
+        default_plot_kind = session['editor-sessions'][self.editor_handle]['datasets'][k]['plot_kind'] # FIXME: Implement multiple plot modes within Plot
806
+        self.kind = poobrains.form.fields.Select(choices=[(kind, cls.__name__) for (kind, cls) in plot_kinds.items()], default=default_plot_kind)
784 807
         self.apply = poobrains.form.Button(type='submit', label='Apply')
785 808
 
786 809
 
@@ -811,14 +834,23 @@ class DataEditor(poobrains.auth.ProtectedForm):
811 834
 
812 835
             editor_session = session['editor-sessions'][self.handle_string]
813 836
 
814
-            self.dataset = Dataset()
815
-            self.dataset.name = self.handle_string
816
-            self.dataset.plot_kind = editor_session['plot_kind'] or 'scatter'
817
-            self.dataset.title = editor_session['title']
818
-            self.dataset.description = editor_session['description']
819
-            self.dataset.data = pandas.DataFrame.from_dict(editor_session['data'])
837
+            self.datasets = {}
838
+
839
+            for dataset_name in editor_session['datasets']:
820 840
 
821
-            if len(self.dataset.data):
841
+                dataset = Dataset()
842
+                dataset.name = dataset_name
843
+                dataset.plot_kind = editor_session['datasets'][dataset_name]['plot_kind'] or 'scatter'
844
+                dataset.title = editor_session['datasets'][dataset_name]['title']
845
+                dataset.description = editor_session['datasets'][dataset_name]['description']
846
+                dataset.data = pandas.DataFrame.from_dict(editor_session['datasets'][dataset_name]['data'])
847
+
848
+                self.datasets[dataset_name] = dataset
849
+
850
+            self.plot = Plot(datasets=list(self.datasets.values()))
851
+
852
+
853
+            if len(self.datasets):
822 854
                 self.plot_kind = PlotKindFieldset(self.handle_string)
823 855
 
824 856
             if editor_session['action'] is None:
@@ -840,7 +872,7 @@ class DataEditor(poobrains.auth.ProtectedForm):
840 872
 
841 873
         elif submit.startswith('load.'):
842 874
             self.fields['load'].process(submit, self)
843
-            if len(self.dataset.data):
875
+            if len(self.datasets):
844 876
                 self.plot_kind = PlotKindFieldset(self.handle_string)
845 877
             #session[self.handle_string] = self.data
846 878
 
@@ -848,8 +880,13 @@ class DataEditor(poobrains.auth.ProtectedForm):
848 880
             self.fields['plot_kind'].process(submit, self)
849 881
 
850 882
         elif submit == 'save_dataset':
851
-            stored_ds = self.dataset.save()
852
-            return redirect(stored_ds.url('edit'))
883
+            #stored_ds = self.dataset.save()
884
+            #return redirect(stored_ds.url('edit'))
885
+
886
+            for dataset in self.datasets.values():
887
+                if dataset.save():
888
+                    flash('Saved dataset %s' % dataset.name)
889
+
853 890
 
854 891
         elif submit == 'delete_session':
855 892
             del(session['editor-sessions'][self.handle_string])

+ 16
- 11
poobrains/themes/default/form/dataeditor.jinja View File

@@ -1,17 +1,22 @@
1 1
 {% extends 'form/form.jinja' %}
2 2
 
3 3
 {% block fields %}
4
-    {% if content.dataset %}
5
-    {% if 'plot_kind' in content.fields %}
6
-        {{ content.fields['plot_kind'].render() }}
7
-    {% endif %}
8
-    <div class="dataeditor-plot">
9
-        {{ content.dataset.plot() }}
10
-    </div>
11
-    <details>
12
-        <summary>Data</summary>
13
-        <code>{{ content.dataset.data }}</code>
14
-    </details>
4
+    {% if content.datasets|length %}
5
+        {% if 'plot_kind' in content.fields %}
6
+            {{ content.fields['plot_kind'].render() }}
7
+        {% endif %}
8
+        <div class="dataeditor-plot">
9
+            {# content.dataset.plot() #}
10
+            FNORD
11
+            {{ content.plot.render('raw') }}
12
+            FNORD
13
+        </div>
14
+        {% for dataset in content.datasets.values() %}
15
+            <details>
16
+                <summary>{{ dataset.title }}</summary>
17
+                <code>{{ dataset.data }}</code>
18
+            </details>
19
+        {% endfor %}
15 20
     {% endif %}
16 21
 
17 22
     {{ content.render_fields() }}

Loading…
Cancel
Save