euporie 2.8.4__py3-none-any.whl → 2.8.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. euporie/console/_commands.py +143 -0
  2. euporie/console/_settings.py +58 -0
  3. euporie/console/app.py +25 -71
  4. euporie/console/tabs/console.py +58 -62
  5. euporie/core/__init__.py +1 -1
  6. euporie/core/__main__.py +28 -11
  7. euporie/core/_settings.py +109 -0
  8. euporie/core/app/__init__.py +3 -0
  9. euporie/core/app/_commands.py +95 -0
  10. euporie/core/app/_settings.py +457 -0
  11. euporie/core/{app.py → app/app.py} +212 -576
  12. euporie/core/app/base.py +51 -0
  13. euporie/core/{current.py → app/current.py} +13 -4
  14. euporie/core/app/cursor.py +35 -0
  15. euporie/core/app/dummy.py +12 -0
  16. euporie/core/app/launch.py +28 -0
  17. euporie/core/bars/__init__.py +11 -0
  18. euporie/core/bars/command.py +205 -0
  19. euporie/core/bars/menu.py +258 -0
  20. euporie/core/{widgets → bars}/search.py +20 -16
  21. euporie/core/{widgets → bars}/status.py +6 -23
  22. euporie/core/clipboard.py +19 -80
  23. euporie/core/comm/base.py +8 -6
  24. euporie/core/comm/ipywidgets.py +16 -7
  25. euporie/core/comm/registry.py +2 -1
  26. euporie/core/commands.py +10 -20
  27. euporie/core/completion.py +3 -2
  28. euporie/core/config.py +368 -341
  29. euporie/core/convert/__init__.py +0 -30
  30. euporie/core/convert/datum.py +116 -53
  31. euporie/core/convert/formats/__init__.py +31 -0
  32. euporie/core/convert/formats/ansi.py +9 -23
  33. euporie/core/convert/formats/common.py +11 -23
  34. euporie/core/convert/formats/html.py +45 -40
  35. euporie/core/convert/formats/pil.py +1 -1
  36. euporie/core/convert/formats/png.py +3 -5
  37. euporie/core/convert/formats/sixel.py +3 -3
  38. euporie/core/convert/registry.py +4 -6
  39. euporie/core/convert/utils.py +41 -4
  40. euporie/core/diagnostics.py +2 -2
  41. euporie/core/filters.py +98 -40
  42. euporie/core/format.py +2 -3
  43. euporie/core/ft/ansi.py +1 -1
  44. euporie/core/ft/html.py +12 -21
  45. euporie/core/ft/table.py +1 -3
  46. euporie/core/ft/utils.py +4 -1
  47. euporie/core/graphics.py +386 -133
  48. euporie/core/history.py +2 -2
  49. euporie/core/inspection.py +3 -2
  50. euporie/core/io.py +207 -28
  51. euporie/core/kernel/__init__.py +1 -0
  52. euporie/core/{kernel.py → kernel/client.py} +45 -108
  53. euporie/core/kernel/manager.py +114 -0
  54. euporie/core/key_binding/bindings/__init__.py +1 -8
  55. euporie/core/key_binding/bindings/basic.py +47 -7
  56. euporie/core/key_binding/bindings/completion.py +3 -8
  57. euporie/core/key_binding/bindings/micro.py +1 -6
  58. euporie/core/key_binding/bindings/mouse.py +2 -2
  59. euporie/core/key_binding/bindings/terminal.py +193 -0
  60. euporie/core/key_binding/key_processor.py +43 -2
  61. euporie/core/key_binding/registry.py +2 -0
  62. euporie/core/key_binding/utils.py +22 -2
  63. euporie/core/keys.py +7156 -93
  64. euporie/core/layout/cache.py +3 -3
  65. euporie/core/layout/containers.py +48 -4
  66. euporie/core/layout/decor.py +2 -2
  67. euporie/core/layout/mouse.py +1 -1
  68. euporie/core/layout/print.py +2 -1
  69. euporie/core/layout/scroll.py +39 -34
  70. euporie/core/log.py +76 -64
  71. euporie/core/lsp.py +118 -24
  72. euporie/core/margins.py +1 -1
  73. euporie/core/path.py +62 -13
  74. euporie/core/renderer.py +58 -17
  75. euporie/core/style.py +57 -39
  76. euporie/core/suggest.py +103 -85
  77. euporie/core/tabs/__init__.py +32 -0
  78. euporie/core/tabs/_settings.py +113 -0
  79. euporie/core/tabs/base.py +80 -470
  80. euporie/core/tabs/kernel.py +419 -0
  81. euporie/core/tabs/notebook.py +24 -101
  82. euporie/core/utils.py +92 -15
  83. euporie/core/validation.py +1 -1
  84. euporie/core/widgets/_settings.py +188 -0
  85. euporie/core/widgets/cell.py +19 -50
  86. euporie/core/widgets/cell_outputs.py +25 -36
  87. euporie/core/widgets/decor.py +11 -41
  88. euporie/core/widgets/dialog.py +62 -27
  89. euporie/core/widgets/display.py +12 -15
  90. euporie/core/widgets/file_browser.py +2 -23
  91. euporie/core/widgets/forms.py +8 -5
  92. euporie/core/widgets/inputs.py +13 -70
  93. euporie/core/widgets/layout.py +2 -1
  94. euporie/core/widgets/logo.py +49 -0
  95. euporie/core/widgets/menu.py +10 -8
  96. euporie/core/widgets/pager.py +6 -10
  97. euporie/core/widgets/palette.py +6 -6
  98. euporie/hub/app.py +52 -35
  99. euporie/notebook/_commands.py +24 -0
  100. euporie/notebook/_settings.py +107 -0
  101. euporie/notebook/app.py +49 -171
  102. euporie/notebook/filters.py +1 -1
  103. euporie/notebook/tabs/__init__.py +46 -7
  104. euporie/notebook/tabs/_commands.py +714 -0
  105. euporie/notebook/tabs/_settings.py +32 -0
  106. euporie/notebook/tabs/display.py +4 -4
  107. euporie/notebook/tabs/edit.py +11 -44
  108. euporie/notebook/tabs/json.py +5 -5
  109. euporie/notebook/tabs/log.py +1 -18
  110. euporie/notebook/tabs/notebook.py +11 -660
  111. euporie/notebook/widgets/_commands.py +11 -0
  112. euporie/notebook/widgets/_settings.py +19 -0
  113. euporie/notebook/widgets/side_bar.py +14 -34
  114. euporie/preview/_settings.py +104 -0
  115. euporie/preview/app.py +6 -31
  116. euporie/preview/tabs/notebook.py +6 -72
  117. euporie/web/__init__.py +1 -0
  118. euporie/web/tabs/__init__.py +14 -0
  119. euporie/web/tabs/web.py +11 -6
  120. euporie/web/widgets/__init__.py +1 -0
  121. euporie/web/widgets/webview.py +5 -15
  122. {euporie-2.8.4.dist-info → euporie-2.8.6.dist-info}/METADATA +10 -8
  123. euporie-2.8.6.dist-info/RECORD +175 -0
  124. {euporie-2.8.4.dist-info → euporie-2.8.6.dist-info}/WHEEL +1 -1
  125. {euporie-2.8.4.dist-info → euporie-2.8.6.dist-info}/entry_points.txt +2 -2
  126. {euporie-2.8.4.dist-info → euporie-2.8.6.dist-info}/licenses/LICENSE +1 -1
  127. euporie/core/launch.py +0 -64
  128. euporie/core/terminal.py +0 -522
  129. euporie-2.8.4.dist-info/RECORD +0 -147
  130. {euporie-2.8.4.data → euporie-2.8.6.data}/data/share/applications/euporie-console.desktop +0 -0
  131. {euporie-2.8.4.data → euporie-2.8.6.data}/data/share/applications/euporie-notebook.desktop +0 -0
@@ -12,24 +12,14 @@ import nbformat
12
12
  from prompt_toolkit.clipboard.base import ClipboardData
13
13
  from prompt_toolkit.filters import (
14
14
  Condition,
15
- buffer_has_focus,
16
- has_completions,
17
- vi_mode,
18
- vi_navigation_mode,
19
15
  )
20
16
  from prompt_toolkit.layout.containers import ConditionalContainer, VSplit
21
17
  from prompt_toolkit.layout.dimension import Dimension
22
18
  from prompt_toolkit.mouse_events import MouseEventType
23
19
 
24
- from euporie.core.commands import add_cmd, get_cmd
25
- from euporie.core.config import add_setting
26
- from euporie.core.current import get_app
20
+ from euporie.core.commands import get_cmd
27
21
  from euporie.core.filters import (
28
- cursor_on_first_line,
29
- cursor_on_last_line,
30
- display_has_focus,
31
22
  insert_mode,
32
- kernel_tab_has_focus,
33
23
  multiple_cells_selected,
34
24
  replace_mode,
35
25
  )
@@ -42,29 +32,23 @@ from euporie.core.layout.decor import Line, Pattern
42
32
  from euporie.core.layout.scroll import ScrollingContainer
43
33
  from euporie.core.margins import MarginContainer, ScrollbarMargin
44
34
  from euporie.core.style import KERNEL_STATUS_REPR
45
- from euporie.core.tabs.base import KernelTab
46
35
  from euporie.core.tabs.notebook import BaseNotebook
47
36
  from euporie.core.widgets.cell import Cell
48
- from euporie.notebook.filters import (
49
- cell_has_focus,
50
- code_cell_selected,
51
- deleted_cells,
52
- notebook_has_focus,
53
- )
54
37
 
55
38
  if TYPE_CHECKING:
39
+ from collections.abc import MutableSequence, Sequence
56
40
  from pathlib import Path
57
- from typing import Any, MutableSequence, Sequence
41
+ from typing import Any
58
42
 
59
43
  from prompt_toolkit.formatted_text.base import StyleAndTextTuples
60
44
  from prompt_toolkit.key_binding.key_bindings import NotImplementedOrNone
61
45
  from prompt_toolkit.layout.containers import AnyContainer
62
46
  from prompt_toolkit.mouse_events import MouseEvent
63
47
 
64
- from euporie.core.app import BaseApp
48
+ from euporie.core.app.app import BaseApp
49
+ from euporie.core.bars.status import StatusBarFields
65
50
  from euporie.core.comm.base import Comm
66
- from euporie.core.kernel import Kernel
67
- from euporie.core.widgets.status import StatusBarFields
51
+ from euporie.core.kernel.client import Kernel
68
52
 
69
53
  log = logging.getLogger(__name__)
70
54
 
@@ -87,6 +71,7 @@ class Notebook(BaseNotebook):
87
71
  file_extensions.update(dict.fromkeys(NOTEBOOK_EXTENSIONS))
88
72
 
89
73
  allow_stdin = True
74
+ bg_init = True
90
75
 
91
76
  def __init__(
92
77
  self,
@@ -139,7 +124,7 @@ class Notebook(BaseNotebook):
139
124
  return (
140
125
  [
141
126
  self.mode(),
142
- f"Cell {self.page.selected_slice.start+1}",
127
+ f"Cell {self.page.selected_slice.start + 1}",
143
128
  f"Rendering… ({rendered:.0%})" if rendered and rendered < 1 else "",
144
129
  "Saving…" if self.saving else "",
145
130
  ],
@@ -183,6 +168,9 @@ class Notebook(BaseNotebook):
183
168
  self.page = ScrollingContainer(
184
169
  self.rendered_cells, width=self.app.config.max_notebook_width
185
170
  )
171
+ # Ensure all cells get initialized ASAP to prevent race conditions
172
+ # creating multiple version of cells across threads
173
+ self.page.all_children()
186
174
 
187
175
  expand = Condition(lambda: self.app.config.expand)
188
176
 
@@ -592,643 +580,6 @@ class Notebook(BaseNotebook):
592
580
  cell.run_or_render(wait=wait)
593
581
  log.debug("All cells run")
594
582
 
595
- # ################################### Settings ####################################
596
-
597
- add_setting(
598
- name="show_scroll_bar",
599
- title="scroll bar",
600
- flags=["--show-scroll-bar"],
601
- type_=bool,
602
- help_="Show the scroll bar",
603
- default=True,
604
- description="""
605
- Whether the scroll bar should be shown on the right of the screen.
606
- """,
607
- )
608
-
609
- # ################################### Commands ####################################
610
-
611
- @staticmethod
612
- @add_cmd(
613
- filter=cell_has_focus & ~buffer_has_focus,
614
- )
615
- def _enter_cell_edit_mode() -> None:
616
- """Enter cell edit mode."""
617
- nb = get_app().tab
618
- if isinstance(nb, Notebook):
619
- nb.enter_edit_mode()
620
-
621
- @staticmethod
622
- @add_cmd(
623
- filter=cell_has_focus
624
- & buffer_has_focus
625
- & (~vi_mode | (vi_mode & vi_navigation_mode)),
626
- )
627
- def _exit_edit_mode() -> None:
628
- """Exit cell edit mode."""
629
- nb = get_app().tab
630
- if isinstance(nb, Notebook):
631
- nb.exit_edit_mode()
632
-
633
- @staticmethod
634
- @add_cmd(
635
- filter=cell_has_focus,
636
- )
637
- def _run_selected_cells() -> None:
638
- """Run or render the current cells."""
639
- nb = get_app().tab
640
- if isinstance(nb, Notebook):
641
- nb.run_selected_cells()
642
-
643
- @staticmethod
644
- @add_cmd(
645
- title="Run selected cells and select next cell",
646
- filter=cell_has_focus,
647
- )
648
- def _run_and_select_next() -> None:
649
- """Run or render the current cells and select the next cell."""
650
- nb = get_app().tab
651
- if isinstance(nb, Notebook):
652
- nb.run_selected_cells(advance=True)
653
-
654
- @staticmethod
655
- @add_cmd(
656
- filter=cell_has_focus,
657
- )
658
- def _run_cell_and_insert_below() -> None:
659
- """Run or render the current cells and insert a new cell below."""
660
- nb = get_app().tab
661
- if isinstance(nb, Notebook):
662
- nb.run_selected_cells(insert=True)
663
-
664
- @staticmethod
665
- @add_cmd(
666
- filter=notebook_has_focus,
667
- )
668
- def _run_all_cells() -> None:
669
- """Run or render all the cells in the current notebook."""
670
- nb = get_app().tab
671
- if isinstance(nb, Notebook):
672
- nb.run_all()
673
-
674
- @staticmethod
675
- @add_cmd(
676
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
677
- )
678
- def _add_cell_above() -> None:
679
- """Add a new cell above the current."""
680
- nb = get_app().tab
681
- if isinstance(nb, Notebook):
682
- nb.add_cell_above()
683
-
684
- @staticmethod
685
- @add_cmd(
686
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
687
- )
688
- def _add_cell_below() -> None:
689
- """Add a new cell below the current."""
690
- nb = get_app().tab
691
- if isinstance(nb, Notebook):
692
- nb.add_cell_below()
693
-
694
- @staticmethod
695
- @add_cmd(
696
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
697
- )
698
- def _delete_cells() -> None:
699
- """Delete the current cells."""
700
- nb = get_app().tab
701
- if isinstance(nb, Notebook):
702
- nb.delete()
703
-
704
- @staticmethod
705
- @add_cmd(
706
- menu_title="Undo delete cell",
707
- filter=notebook_has_focus
708
- & ~buffer_has_focus
709
- & ~display_has_focus
710
- & deleted_cells,
711
- )
712
- def _undelete_cells() -> None:
713
- """Undelete the last deleted cells."""
714
- nb = get_app().tab
715
- if isinstance(nb, Notebook):
716
- nb.undelete()
717
-
718
- @staticmethod
719
- @add_cmd(
720
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
721
- )
722
- def _cut_cells() -> None:
723
- """Cut the current cells."""
724
- nb = get_app().tab
725
- if isinstance(nb, Notebook):
726
- nb.cut()
727
-
728
- @staticmethod
729
- @add_cmd(
730
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
731
- )
732
- def _copy_cells() -> None:
733
- """Copy the current cells."""
734
- nb = get_app().tab
735
- if isinstance(nb, Notebook):
736
- nb.copy()
737
-
738
- @staticmethod
739
- @add_cmd(
740
- menu_title="Copy cell outputs",
741
- filter=cell_has_focus & ~buffer_has_focus,
742
- )
743
- def _copy_outputs() -> None:
744
- """Copy the cell's output to the clipboard."""
745
- nb = get_app().tab
746
- if isinstance(nb, Notebook):
747
- nb.copy_outputs()
748
-
749
- @staticmethod
750
- @add_cmd(
751
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
752
- )
753
- def _paste_cells() -> None:
754
- """Pate the previously copied cells."""
755
- nb = get_app().tab
756
- if isinstance(nb, Notebook):
757
- nb.paste()
758
-
759
- @staticmethod
760
- @add_cmd(
761
- filter=notebook_has_focus
762
- & ~buffer_has_focus
763
- & ~display_has_focus
764
- & multiple_cells_selected,
765
- )
766
- def _merge_cells() -> None:
767
- """Merge the selected cells."""
768
- nb = get_app().tab
769
- if isinstance(nb, Notebook):
770
- nb.merge()
771
-
772
- @staticmethod
773
- @add_cmd(
774
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
775
- )
776
- @add_cmd(
777
- filter=notebook_has_focus,
778
- )
779
- def _scroll_up() -> NotImplementedOrNone:
780
- """Scroll the page up a line."""
781
- nb = get_app().tab
782
- if isinstance(nb, Notebook):
783
- return nb.page.scroll(1)
784
- return None
785
-
786
- @staticmethod
787
- @add_cmd(
788
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
789
- )
790
- @add_cmd(
791
- filter=notebook_has_focus,
792
- )
793
- def _scroll_down() -> NotImplementedOrNone:
794
- """Scroll the page down a line."""
795
- nb = get_app().tab
796
- if isinstance(nb, Notebook):
797
- return nb.page.scroll(-1)
798
- return None
799
-
800
- @staticmethod
801
- @add_cmd(
802
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
803
- )
804
- def _scroll_up_5_lines() -> None:
805
- """Scroll the page up 5 lines."""
806
- nb = get_app().tab
807
- if isinstance(nb, Notebook):
808
- nb.page.scroll(5)
809
-
810
- @staticmethod
811
- @add_cmd(
812
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
813
- )
814
- def _scroll_down_5_lines() -> None:
815
- """Scroll the page down 5 lines."""
816
- nb = get_app().tab
817
- if isinstance(nb, Notebook):
818
- nb.page.scroll(-5)
819
-
820
- @staticmethod
821
- @add_cmd(
822
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
823
- )
824
- def _select_first_cell() -> None:
825
- """Select the first cell in the notebook."""
826
- nb = get_app().tab
827
- if isinstance(nb, Notebook):
828
- nb.select(0)
829
-
830
- @staticmethod
831
- @add_cmd(
832
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
833
- )
834
- def _select_5th_previous_cell() -> None:
835
- """Go up 5 cells."""
836
- nb = get_app().tab
837
- if isinstance(nb, Notebook):
838
- nb.select(nb.page.selected_slice.start - 5)
839
-
840
- @staticmethod
841
- @add_cmd(
842
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
843
- )
844
- def _select_previous_cell() -> None:
845
- """Go up one cell."""
846
- nb = get_app().tab
847
- if isinstance(nb, Notebook):
848
- nb.select(nb.page.selected_slice.start - 1)
849
-
850
- @staticmethod
851
- @add_cmd(
852
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
853
- )
854
- def _select_next_cell() -> None:
855
- """Select the next cell."""
856
- nb = get_app().tab
857
- if isinstance(nb, Notebook):
858
- nb.select(nb.page.selected_slice.start + 1)
859
-
860
- @staticmethod
861
- @add_cmd(
862
- filter=~buffer_has_focus,
863
- )
864
- def _select_5th_next_cell() -> None:
865
- """Go down 5 cells."""
866
- nb = get_app().tab
867
- if isinstance(nb, Notebook):
868
- nb.select(nb.page.selected_slice.start + 5)
869
-
870
- @staticmethod
871
- @add_cmd(
872
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
873
- )
874
- def _select_last_cell() -> None:
875
- """Select the last cell in the notebook."""
876
- nb = get_app().tab
877
- if isinstance(nb, Notebook):
878
- nb.select(len(nb.page.all_children()) - 1)
879
-
880
- @staticmethod
881
- @add_cmd(
882
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
883
- )
884
- def _select_all_cells() -> None:
885
- """Select all cells in the notebook."""
886
- nb = get_app().tab
887
- if isinstance(nb, Notebook):
888
- nb.page.selected_slice = slice(
889
- 0,
890
- len(nb.page.all_children()) + 1,
891
- )
892
-
893
- @staticmethod
894
- @add_cmd(
895
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
896
- )
897
- def _extend_cell_selection_to_top() -> None:
898
- """Extend the cell selection to the top of the notebook."""
899
- nb = get_app().tab
900
- if isinstance(nb, Notebook):
901
- nb.select(0, extend=True)
902
-
903
- @staticmethod
904
- @add_cmd(
905
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
906
- )
907
- def _extend_cell_selection_up() -> None:
908
- """Extend the cell selection up a cell."""
909
- nb = get_app().tab
910
- if isinstance(nb, Notebook):
911
- nb.select(nb.page._selected_slice.start - 1, extend=True)
912
-
913
- @staticmethod
914
- @add_cmd(
915
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
916
- )
917
- def _extend_cell_selection_down() -> None:
918
- """Extend the cell selection down a cell."""
919
- nb = get_app().tab
920
- if isinstance(nb, Notebook):
921
- nb.select(nb.page._selected_slice.start + 1, extend=True)
922
-
923
- @staticmethod
924
- @add_cmd(
925
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
926
- )
927
- def _extend_cell_selection_to_bottom() -> None:
928
- """Extend the cell selection to the bottom of the notebook."""
929
- nb = get_app().tab
930
- if isinstance(nb, Notebook):
931
- nb.select(len(nb.json["cells"]) - 1, extend=True)
932
-
933
- @staticmethod
934
- @add_cmd(
935
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
936
- )
937
- def _move_cells_up() -> None:
938
- """Move selected cells up."""
939
- nb = get_app().tab
940
- if isinstance(nb, Notebook):
941
- nb.move(-1)
942
-
943
- @staticmethod
944
- @add_cmd(
945
- filter=notebook_has_focus & ~buffer_has_focus & ~display_has_focus,
946
- )
947
- def _move_cells_down() -> None:
948
- """Move selected cells down."""
949
- nb = get_app().tab
950
- if isinstance(nb, Notebook):
951
- nb.move(1)
952
-
953
- @staticmethod
954
- @add_cmd(
955
- filter=cell_has_focus & ~buffer_has_focus,
956
- )
957
- def _cells_to_markdown() -> None:
958
- """Change selected cells to markdown cells."""
959
- nb = get_app().tab
960
- if isinstance(nb, Notebook):
961
- for cell in nb.cells:
962
- cell.set_cell_type("markdown", clear=True)
963
- # Remove unallowed additional properties
964
- json = cell.json
965
- json.pop("execution_count", None)
966
- json.pop("outputs", None)
967
- cell.run_or_render()
968
-
969
- @staticmethod
970
- @add_cmd(
971
- filter=cell_has_focus & ~buffer_has_focus,
972
- )
973
- def _cells_to_code() -> None:
974
- """Change selected cells to code cells."""
975
- nb = get_app().tab
976
- if isinstance(nb, Notebook):
977
- for cell in nb.cells:
978
- cell.set_cell_type("code", clear=False)
979
-
980
- @staticmethod
981
- @add_cmd(
982
- filter=cell_has_focus & ~buffer_has_focus,
983
- )
984
- def _cells_to_raw() -> None:
985
- """Change selected cells to raw cells."""
986
- nb = get_app().tab
987
- if isinstance(nb, Notebook):
988
- for cell in nb.cells:
989
- cell.set_cell_type("raw", clear=True)
990
-
991
- @staticmethod
992
- @add_cmd(
993
- filter=cell_has_focus & ~buffer_has_focus,
994
- )
995
- def _clear_cell_outputs() -> None:
996
- """Clear the outputs of the selected cells."""
997
- nb = get_app().tab
998
- if isinstance(nb, Notebook):
999
- for cell in nb.cells:
1000
- cell.remove_outputs()
1001
-
1002
- @staticmethod
1003
- @add_cmd(
1004
- filter=cell_has_focus & ~buffer_has_focus,
1005
- )
1006
- def _clear_all_outputs() -> None:
1007
- """Clear the outputs of the selected cells."""
1008
- nb = get_app().tab
1009
- if isinstance(nb, Notebook):
1010
- for cell in nb._rendered_cells.values():
1011
- cell.remove_outputs()
1012
-
1013
- @staticmethod
1014
- @add_cmd(
1015
- filter=cell_has_focus & ~buffer_has_focus,
1016
- title="Expand cell inputs",
1017
- )
1018
- def _show_cell_inputs() -> None:
1019
- """Expand the selected cells' inputs."""
1020
- nb = get_app().tab
1021
- if isinstance(nb, Notebook):
1022
- for cell in nb.cells:
1023
- cell.show_input()
1024
-
1025
- @staticmethod
1026
- @add_cmd(
1027
- filter=cell_has_focus & ~buffer_has_focus,
1028
- title="Collapse cell inputs",
1029
- )
1030
- def _hide_cell_inputs() -> None:
1031
- """Collapse the selected cells' inputs."""
1032
- nb = get_app().tab
1033
- if isinstance(nb, Notebook):
1034
- for cell in nb.cells:
1035
- cell.hide_input()
1036
-
1037
- @staticmethod
1038
- @add_cmd(
1039
- filter=cell_has_focus & ~buffer_has_focus,
1040
- )
1041
- def _toggle_cell_inputs() -> None:
1042
- """Toggle the visibility of the selected cells' inputs."""
1043
- nb = get_app().tab
1044
- if isinstance(nb, Notebook):
1045
- for cell in nb.cells:
1046
- cell.toggle_input()
1047
-
1048
- @staticmethod
1049
- @add_cmd(
1050
- filter=cell_has_focus & ~buffer_has_focus,
1051
- title="Expand cell outputs",
1052
- )
1053
- def _show_cell_outputs() -> None:
1054
- """Expand the selected cells' outputs."""
1055
- nb = get_app().tab
1056
- if isinstance(nb, Notebook):
1057
- for cell in nb.cells:
1058
- cell.show_output()
1059
-
1060
- @staticmethod
1061
- @add_cmd(
1062
- filter=cell_has_focus & ~buffer_has_focus,
1063
- title="Collapse cell outputs",
1064
- )
1065
- def _hide_cell_outputs() -> None:
1066
- """Collapse the selected cells' outputs."""
1067
- nb = get_app().tab
1068
- if isinstance(nb, Notebook):
1069
- for cell in nb.cells:
1070
- cell.hide_output()
1071
-
1072
- @staticmethod
1073
- @add_cmd(
1074
- filter=cell_has_focus & ~buffer_has_focus,
1075
- )
1076
- def _toggle_cell_outputs() -> None:
1077
- """Toggle the visibility of the selected cells' outputs."""
1078
- nb = get_app().tab
1079
- if isinstance(nb, Notebook):
1080
- for cell in nb.cells:
1081
- cell.toggle_output()
1082
-
1083
- @staticmethod
1084
- @add_cmd(
1085
- title="Reformat cells",
1086
- filter=code_cell_selected
1087
- & cell_has_focus
1088
- & ~buffer_has_focus
1089
- & notebook_has_focus,
1090
- )
1091
- def _reformat_cells() -> None:
1092
- """Format the selected code cells."""
1093
- nb = get_app().tab
1094
- if isinstance(nb, Notebook):
1095
- for cell in nb.cells:
1096
- if cell.cell_type == "code":
1097
- cell.input_box.reformat()
1098
-
1099
- @staticmethod
1100
- @add_cmd(filter=notebook_has_focus & ~buffer_has_focus)
1101
- def _reformat_notebook() -> None:
1102
- """Automatically reformat all code cells in the notebook."""
1103
- nb = get_app().tab
1104
- if isinstance(nb, Notebook):
1105
- nb.reformat()
1106
-
1107
- @staticmethod
1108
- @add_cmd(
1109
- filter=cell_has_focus & ~buffer_has_focus,
1110
- )
1111
- async def _edit_in_external_editor() -> None:
1112
- """Edit cell in $EDITOR."""
1113
- nb = get_app().tab
1114
- if isinstance(nb, Notebook):
1115
- await nb.cell.edit_in_editor()
1116
-
1117
- @staticmethod
1118
- @add_cmd(
1119
- filter=cell_has_focus & buffer_has_focus,
1120
- )
1121
- def _split_cell() -> None:
1122
- """Split the current cell at the cursor position."""
1123
- nb = get_app().tab
1124
- if isinstance(nb, Notebook):
1125
- nb.split_cell()
1126
-
1127
- @staticmethod
1128
- @add_cmd(
1129
- name="edit-previous-cell-vi",
1130
- filter=cell_has_focus
1131
- & buffer_has_focus
1132
- & cursor_on_first_line
1133
- & ~has_completions
1134
- & vi_mode
1135
- & vi_navigation_mode,
1136
- )
1137
- @add_cmd(
1138
- filter=cell_has_focus
1139
- & buffer_has_focus
1140
- & cursor_on_first_line
1141
- & ~has_completions
1142
- )
1143
- def _edit_previous_cell() -> None:
1144
- """Move the cursor up to the previous cell."""
1145
- nb = get_app().tab
1146
- if isinstance(nb, Notebook):
1147
- new_index = nb.cell.index - 1
1148
- cells = nb.rendered_cells()
1149
- if 0 <= new_index < len(cells):
1150
- nb.select(index=new_index, position=-1, scroll=True)
1151
-
1152
- @staticmethod
1153
- @add_cmd(
1154
- name="edit-next-cell-vi",
1155
- filter=cell_has_focus
1156
- & buffer_has_focus
1157
- & cursor_on_last_line
1158
- & ~has_completions
1159
- & vi_mode
1160
- & vi_navigation_mode,
1161
- )
1162
- @add_cmd(
1163
- filter=cell_has_focus
1164
- & buffer_has_focus
1165
- & cursor_on_last_line
1166
- & ~has_completions
1167
- )
1168
- def _edit_next_cell() -> None:
1169
- """Move the cursor down to the next cell."""
1170
- nb = get_app().tab
1171
- if isinstance(nb, Notebook):
1172
- new_index = nb.cell.index + 1
1173
- cells = nb.rendered_cells()
1174
- if 0 <= new_index < len(cells):
1175
- nb.select(index=new_index, position=0, scroll=True)
1176
-
1177
- @staticmethod
1178
- @add_cmd(
1179
- filter=cell_has_focus & ~buffer_has_focus,
1180
- )
1181
- def _scroll_output_left() -> None:
1182
- """Scroll the cell output to the left."""
1183
- nb = get_app().tab
1184
- if isinstance(nb, Notebook):
1185
- nb.cell.output_area.scroll_left()
1186
-
1187
- @staticmethod
1188
- @add_cmd(
1189
- filter=cell_has_focus & ~buffer_has_focus,
1190
- )
1191
- def _scroll_output_right() -> None:
1192
- """Scroll the cell output to the right."""
1193
- nb = get_app().tab
1194
- if isinstance(nb, Notebook):
1195
- nb.cell.output_area.scroll_right()
1196
-
1197
- @staticmethod
1198
- @add_cmd(
1199
- filter=kernel_tab_has_focus & ~buffer_has_focus & ~display_has_focus,
1200
- )
1201
- def _interrupt_kernel() -> None:
1202
- """Interrupt the notebook's kernel."""
1203
- if isinstance(kt := get_app().tab, KernelTab):
1204
- kt.interrupt_kernel()
1205
-
1206
- @staticmethod
1207
- @add_cmd(
1208
- filter=kernel_tab_has_focus & ~buffer_has_focus & ~display_has_focus,
1209
- )
1210
- def _restart_kernel() -> None:
1211
- """Restart the notebook's kernel."""
1212
- if isinstance(kt := get_app().tab, KernelTab):
1213
- kt.restart_kernel()
1214
-
1215
- @staticmethod
1216
- @add_cmd(
1217
- filter=kernel_tab_has_focus & ~buffer_has_focus & ~display_has_focus,
1218
- )
1219
- def _restart_kernel_and_clear_all_outputs() -> None:
1220
- """Restart the notebook's kernel and clear all cell output."""
1221
- if isinstance(nb := get_app().tab, Notebook):
1222
- nb.restart_kernel(cb=Notebook._clear_all_outputs)
1223
-
1224
- @staticmethod
1225
- @add_cmd(
1226
- filter=~buffer_has_focus,
1227
- )
1228
- def _notebook_toggle_line_numbers() -> None:
1229
- """Toggle line numbers when a buffer does not have focus."""
1230
- get_cmd("toggle-line-numbers").run()
1231
-
1232
583
  # ################################# Key Bindings ##################################
1233
584
 
1234
585
  register_bindings(