From 234f2d69756eb226e7a942a824b19e55c6b98f15 Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Tue, 4 Jan 2022 12:35:18 +0100 Subject: [PATCH 1/4] Fix line not defined bug --- opencodeblocks/scene/from_ipynb_conversion.py | 32 ++++++++----------- .../scene/ipynb_conversion_constants.py | 4 +++ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/opencodeblocks/scene/from_ipynb_conversion.py b/opencodeblocks/scene/from_ipynb_conversion.py index 3165d690..aaefbfd9 100644 --- a/opencodeblocks/scene/from_ipynb_conversion.py +++ b/opencodeblocks/scene/from_ipynb_conversion.py @@ -44,7 +44,7 @@ def get_blocks_data( font.setFixedPitch(True) font.setPointSize(POINT_SIZE) fontmetrics = QFontMetrics(font) - + blocks_data: List[OrderedDict] = [] next_block_x_pos: float = 0 @@ -58,28 +58,24 @@ def get_blocks_data( block_type: str = cell["cell_type"] text: str = cell["source"] - - boundingWidth = 10 - if use_theme_font: - boundingWidth = fontmetrics.boundingRect(line).width() - text_width: float = ( - max(boundingWidth for line in text) - if len(text) > 0 - else 0 - ) + text_width = DEFAULT_TEXT_WIDTH + if use_theme_font: + text_width: float = ( + max(fontmetrics.boundingRect(line).width() for line in text) + if len(text) > 0 + else 0 + ) block_width: float = max(text_width + MARGIN_X, BLOCK_MIN_WIDTH) - - lineSpacing = 2 - lineWidth = 10 + + lineSpacing = DEFAULT_LINE_SPACING + lineHeight = DEFAULT_LINE_HEIGHT if use_theme_font: lineSpacing = fontmetrics.lineSpacing() - lineWidth = fontmetrics.lineWidth() - - text_height: float = len(text) * ( - lineSpacing + lineWidth - ) + lineHeight = fontmetrics.lineWidth() + + text_height: float = len(text) * (lineSpacing + lineHeight) block_height: float = text_height + MARGIN_Y block_data = { diff --git a/opencodeblocks/scene/ipynb_conversion_constants.py b/opencodeblocks/scene/ipynb_conversion_constants.py index 78ecba72..65880903 100644 --- a/opencodeblocks/scene/ipynb_conversion_constants.py +++ b/opencodeblocks/scene/ipynb_conversion_constants.py @@ -10,6 +10,10 @@ TITLE_MAX_LENGTH: int = 60 SOCKET_HEIGHT: float = 44.0 +DEFAULT_LINE_SPACING = 2 +DEFAULT_LINE_HEIGHT = 10 +DEFAULT_TEXT_WIDTH = 618 + BLOCK_TYPE_TO_NAME: Dict[str, str] = { "code": "OCBCodeBlock", "markdown": "OCBMarkdownBlock", From 2dc65617912ce0b476f445acdebed5325a543035 Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Tue, 4 Jan 2022 12:53:59 +0100 Subject: [PATCH 2/4] :beetle: Fix save as bug --- opencodeblocks/graphics/widget.py | 4 ++-- opencodeblocks/graphics/window.py | 10 +++++----- tests/assets/usual.ipynb | 3 +-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/opencodeblocks/graphics/widget.py b/opencodeblocks/graphics/widget.py index d5025355..5399ba28 100644 --- a/opencodeblocks/graphics/widget.py +++ b/opencodeblocks/graphics/widget.py @@ -61,9 +61,9 @@ def savepath(self, value: str): def save(self): self.scene.save(self.savepath) - def saveAsJupyter(self): + def saveAsJupyter(self, filepath:str): """Save the current graph notebook as a regular python notebook""" - self.scene.save_to_ipynb(self.savepath) + self.scene.save_to_ipynb(filepath) def load(self, filepath: str): self.scene.load(filepath) diff --git a/opencodeblocks/graphics/window.py b/opencodeblocks/graphics/window.py index e0017f7c..1678fbb6 100644 --- a/opencodeblocks/graphics/window.py +++ b/opencodeblocks/graphics/window.py @@ -129,7 +129,7 @@ def createActions(self): self._actSaveAsJupyter = QAction( "Save &As ... .ipynb", statusTip="Save the ipygraph as a Jupter Notebook at ...", - triggered=self.oneFileSaveAsJupyter, + triggered=self.onFileSaveAsJupyter, ) self._actQuit = QAction( "&Quit", @@ -359,7 +359,7 @@ def onFileSaveAs(self) -> bool: return True return False - def oneFileSaveAsJupyter(self) -> bool: + def onFileSaveAsJupyter(self) -> bool: """Save file in a given directory as ipynb, caching savepath for quick save. Returns: @@ -371,13 +371,13 @@ def oneFileSaveAsJupyter(self) -> bool: dialog = QFileDialog() dialog.setDefaultSuffix(".ipynb") filename, _ = dialog.getSaveFileName( - self, "Save ipygraph to file", filter="IPython Graph (*.ipynb)" + self, "Save ipygraph to file", filter="Jupyter Notebook (*.ipynb)" ) if filename == "": return False - current_window.saveAsJupyter() + current_window.saveAsJupyter(filename) self.statusbar.showMessage( - f"Successfully saved ipygraph as jupter notebook at {current_window.savepath}", + f"Successfully saved ipygraph as jupter notebook at {filename}", 2000, ) return True diff --git a/tests/assets/usual.ipynb b/tests/assets/usual.ipynb index 8f60756f..481bd5b6 100644 --- a/tests/assets/usual.ipynb +++ b/tests/assets/usual.ipynb @@ -25,8 +25,7 @@ ], "source": [ "# Imports\n", - "import numpy as np\n", - "import anotherlib as al" + "import numpy as np" ] }, { From 2aeaaf18e8715464101d0aac17c110f51066ca4e Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Tue, 4 Jan 2022 13:54:02 +0100 Subject: [PATCH 3/4] :wrench: Move default fields as class attributes --- opencodeblocks/blocks/codeblock.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/opencodeblocks/blocks/codeblock.py b/opencodeblocks/blocks/codeblock.py index 229f5681..89368b1d 100644 --- a/opencodeblocks/blocks/codeblock.py +++ b/opencodeblocks/blocks/codeblock.py @@ -37,17 +37,18 @@ class OCBCodeBlock(OCBExecutableBlock): """ + DEFAULT_DATA = { + **OCBBlock.DEFAULT_DATA, + "source": "", + } + MANDATORY_FIELDS = OCBBlock.MANDATORY_FIELDS + def __init__(self, source: str = "", **kwargs): """ Create a new OCBCodeBlock. Initialize all the child widgets specific to this block type """ - DEFAULT_DATA = { - **OCBBlock.DEFAULT_DATA, - "source": "", - } - MANDATORY_FIELDS = OCBBlock.MANDATORY_FIELDS super().__init__(**kwargs) self.source_editor = PythonEditor(self) From 59e7a314f00f3ebb005a4fcc1542da50a89916a7 Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Tue, 4 Jan 2022 17:56:03 +0100 Subject: [PATCH 4/4] :beetle: Fix the run right bug --- opencodeblocks/graphics/edge.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/opencodeblocks/graphics/edge.py b/opencodeblocks/graphics/edge.py index 7b178039..b0a683f4 100644 --- a/opencodeblocks/graphics/edge.py +++ b/opencodeblocks/graphics/edge.py @@ -252,10 +252,8 @@ def deserialize(self, data: OrderedDict, hashmap: dict = None, restore_id=True): self.path_type = data["path_type"] try: self.source_socket = hashmap[data["source"]["socket"]] - self.source_socket.add_edge(self, is_destination=False) self.destination_socket = hashmap[data["destination"]["socket"]] - self.destination_socket.add_edge(self, is_destination=True) self.update_path() except KeyError: self.remove()