File: live_editor.enaml

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (108 lines) | stat: -rw-r--r-- 3,115 bytes parent folder | download
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
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
""" An example of using the live editor applib components.

This examples shows how the various applib live editor components can be
stitched together to form a live Enaml code editor application.

<< autodoc-me >>
"""
import sys
import traceback

from enaml.applib.live_editor_model import LiveEditorModel
from enaml.applib.live_editor_view import (
    ModelEditorPanel, ViewEditorPanel, TracebackPanel, ViewPanel
)
from enaml.layout.api import HSplitLayout, AreaLayout, DockBarLayout
from enaml.widgets.api import Window, Container, DockArea, DockItem


MODEL_TEXT = """
from atom.api import *


class DemoModel(Atom):
    pass

"""


VIEW_TEXT = """
from enaml.widgets.api import *
from enaml.layout.api import *


enamldef DemoContainer(Container):
    pass

"""


enamldef Main(Window):
    title = 'Live Editor Demo'
    initial_size = (1024, 768)
    attr editor_model = LiveEditorModel(
        model_text=MODEL_TEXT,
        model_item='DemoModel',
        model_filename='demo.py',
        view_text=VIEW_TEXT,
        view_item='DemoContainer',
        view_filename='demo.enaml',
    )

    func handle_uncaught_exception(exc, value, tb):
        """Send uncaught exception to the model to avoid crashing the editor."""
        msg = "".join(traceback.format_exception(exc, value, tb))
        print(msg) # Print to console in case there was a startup error
        editor_model.traceback = msg

    initialized::
        sys.excepthook = handle_uncaught_exception

    Container:
        padding = 0
        DockArea:
            layout = AreaLayout(
                HSplitLayout(
                    'view-editor-item',
                    'view-item',
                    sizes=[1, 3],
                ),
                dock_bars=[
                    DockBarLayout(
                        'model-editor-item',
                        'traceback-item',
                        position='left',
                    ),
                ],
            )
            DockItem:
                name = 'model-editor-item'
                title = 'Model Editor'
                stretch = 1
                ModelEditorPanel:
                    model = editor_model
            DockItem:
                name = 'view-editor-item'
                title = 'View Editor'
                stretch = 1
                ViewEditorPanel:
                    model = editor_model
            DockItem:
                name = 'traceback-item'
                title = 'Errors'
                stretch = 1
                TracebackPanel:
                    model = editor_model
            DockItem:
                name = 'view-item'
                title = 'Live View'
                stretch = 4
                ViewPanel:
                    model = editor_model