Debian Codegraph @

traitsui/examples/demo/Standard_Editors/DataFrameEditor_demo.py

raw
1 # (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 # DataFrameEditor_demo.py -- Example of using dataframe editors
12
13 # Dataset from https://www.kaggle.com/mokosan/lord-of-the-rings-character-data
14 """
15 **WARNING**
16
17 This demo might not work as expected and some documented features might be
18 missing.
19
20 -------------------------------------------------------------------------------
21
22 To run this demonstration successfully, you must have **pandas** installed.
23
24 Please refer to the `DataFrameEditor API docs`_ for further information.
25
26 .. _DataFrameEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.ui_editors.data_frame_editor.html#traitsui.ui_editors.data_frame_editor.DataFrameEditor
27 """
28 # Issue related to the demo warning: enthought/traitsui#944
29
30
31 import numpy as np
32 from pandas import DataFrame
33
34 from traits.api import HasTraits, Instance
35
36 from traitsui.api import View, Item
37 from traitsui.ui_editors.data_frame_editor import DataFrameEditor
38
39
40 class DataFrameEditorDemo(HasTraits):
41
42 df = Instance(DataFrame)
43
44 traits_view = View(
45 Item(
46 'df',
47 show_label=False,
48 editor=DataFrameEditor(
49 formats={
50 'RuntimeInMinutes': '%.4d',
51 'BudgetInMillions': '%d',
52 'BoxOfficeRevenueInMillions': '%d',
53 'AcademyAwardNominations': '%d',
54 'AcademyAwardWins': '%d',
55 'RottenTomatoesScore': '%.2f',
56 }
57 ),
58 ),
59 title="DataFrameEditor",
60 resizable=True,
61 id='traitsui.demo.Applications.data_frame_editor_demo',
62 )
63
64
65 # Sample Data
66 lotrMovieData = np.array(
67 [
68 [558, 281, 2917.0, 30, 17, 94.0],
69 [178, 93, 871.5, 13, 4, 91.0],
70 [179, 94, 926.0, 6, 2, 96.0],
71 [201, 94, 1120, 11, 11, 95.0],
72 [462.0, 675, 2932.0, 7, 1, 66.33333333],
73 [169, 200, 1021.0, 3, 1, 64.0],
74 [161, 217, 958.4, 3, 0, 75.0],
75 [144, 250, 956.0, 1, 0, 60.0],
76 ]
77 )
78
79 col_names = [
80 'RuntimeInMinutes',
81 'BudgetInMillions',
82 'BoxOfficeRevenueInMillions',
83 'AcademyAwardNominations',
84 'AcademyAwardWins',
85 'RottenTomatoesScore',
86 ]
87
88 index_names = [
89 'The Lord of the Rings Series',
90 'The Fellowship of the Ring',
91 'The Two Towers ',
92 'The Return of the King',
93 'The Hobbit Series',
94 'The Unexpected Journey',
95 'The Desolation of Smaug',
96 'The Battle of the Five Armies',
97 ]
98
99 # Create & run the demo
100 df = DataFrame(data=lotrMovieData, columns=col_names, index=index_names)
101
102 demo = DataFrameEditorDemo(df=df)
103
104 # Run the demo (if invoked from the command line):
105 if __name__ == '__main__':
106 demo.configure_traits()