Skip to content

Commit

Permalink
Merge pull request #129 from Bycelium/bug/fix_ipynb_conversion
Browse files Browse the repository at this point in the history
🪲 Fix bug related to ipynb conversion
  • Loading branch information
MathisFederico authored Jan 4, 2022
2 parents e89b45a + 59e7a31 commit 5dc13de
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
11 changes: 6 additions & 5 deletions opencodeblocks/blocks/codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,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)
Expand Down
2 changes: 0 additions & 2 deletions opencodeblocks/graphics/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions opencodeblocks/graphics/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions opencodeblocks/graphics/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
32 changes: 14 additions & 18 deletions opencodeblocks/scene/from_ipynb_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down
4 changes: 4 additions & 0 deletions opencodeblocks/scene/ipynb_conversion_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions tests/assets/usual.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
],
"source": [
"# Imports\n",
"import numpy as np\n",
"import anotherlib as al"
"import numpy as np"
]
},
{
Expand Down

0 comments on commit 5dc13de

Please sign in to comment.