Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions blocks/cnn_model.ocbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"title": "CNN",
"block_type": "code",
"source": "input_size = 28\r\nclasses = 5\r\nmodel = Sequential()\r\nmodel.add(layers.Conv2D(input_size, kernel_size=(3,3), input_shape=(input_size,input_size,1)))\r\nmodel.add(layers.MaxPooling2D(pool_size=(2, 2)))\r\nmodel.add(layers.Flatten())\r\nmodel.add(layers.Dense(128, activation=tf.nn.relu))\r\nmodel.add(layers.Dropout(0.2))\r\nmodel.add(layers.Dense(classes,activation=tf.nn.softmax))\r\n\r\nmodel.compile(optimizer='adam', \r\n loss='sparse_categorical_crossentropy')",
"stdout": "",
"image": "",
"splitter_pos": [80,50],
"width": 600,
"height": 400,
"metadata": {
"title_metadata": {
"color": "white",
"font": "Ubuntu",
"size": 10,
"padding": 4.0
}
}
}
18 changes: 18 additions & 0 deletions blocks/empty.ocbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"title": "Empty",
"block_type": "code",
"source": "",
"stdout": "",
"image": "",
"splitter_pos": [88,41],
"width": 618,
"height": 184,
"metadata": {
"title_metadata": {
"color": "white",
"font": "Ubuntu",
"size": 10,
"padding": 4.0
}
}
}
18 changes: 18 additions & 0 deletions blocks/import_ml.ocbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"title": "Imports for ML",
"block_type": "code",
"source": "import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential",
"stdout": "",
"image": "",
"splitter_pos": [80,50],
"width": 600,
"height": 400,
"metadata": {
"title_metadata": {
"color": "white",
"font": "Ubuntu",
"size": 10,
"padding": 4.0
}
}
}
3 changes: 2 additions & 1 deletion opencodeblocks/graphics/blocks/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ def deserialize(self, data: dict, hashmap: dict = None,
socket = OCBSocket(block=self)
socket.deserialize(socket_data, hashmap, restore_id)
self.add_socket(socket)
hashmap.update({socket_data['id']: socket})
if hashmap is not None:
hashmap.update({socket_data['id']: socket})


class OCBSplitterHandle(QSplitterHandle):
Expand Down
2 changes: 1 addition & 1 deletion opencodeblocks/graphics/blocks/blocksizegrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def mousePressEvent(self, mouseEvent: QMouseEvent):
self.mouseY = mouseEvent.globalY()
self.resizing = True

def mouseReleaseEvent(self, mouseEvent: QMouseEvent):
def mouseReleaseEvent(self, mouseEvent: QMouseEvent): # pylint:disable=unused-argument
""" Stop the resizing """
self.resizing = False
self.block.scene().history.checkpoint("Resized block", set_modified=True)
Expand Down
2 changes: 1 addition & 1 deletion opencodeblocks/graphics/blocks/codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def image(self, value: str):
ba = QByteArray.fromBase64(str.encode(self.image))
pixmap = QPixmap()
pixmap.loadFromData(ba)
text = '<img src="data:image/png;base64,{}">'.format(self.image)
text = f'<img src="data:image/png;base64,{self.image}">'
self.output_panel.setText(text)

@source.setter
Expand Down
63 changes: 42 additions & 21 deletions opencodeblocks/graphics/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class OCBScene(QGraphicsScene, Serializable):
""" Scene for the OCB Window. """

def __init__(self, parent=None,
background_color:str="#393939",
grid_color:str="#292929", grid_light_color:str="#2f2f2f",
width:int=64000, height:int=64000,
grid_size:int=20, grid_squares:int=5):
background_color: str = "#393939",
grid_color: str = "#292929", grid_light_color: str = "#2f2f2f",
width: int = 64000, height: int = 64000,
grid_size: int = 20, grid_squares: int = 5):
Serializable.__init__(self)
QGraphicsScene.__init__(self, parent=parent)

Expand All @@ -39,7 +39,8 @@ def __init__(self, parent=None,
self.grid_squares = grid_squares

self.width, self.height = width, height
self.setSceneRect(-self.width//2, -self.height//2, self.width, self.height)
self.setSceneRect(-self.width // 2, -self.height //
2, self.width, self.height)
self.setBackgroundBrush(self._background_color)

self._has_been_modified = False
Expand All @@ -52,13 +53,14 @@ def __init__(self, parent=None,
def has_been_modified(self):
""" True if the scene has been modified, False otherwise. """
return self._has_been_modified

@has_been_modified.setter
def has_been_modified(self, value:bool):
def has_been_modified(self, value: bool):
self._has_been_modified = value
for callback in self._has_been_modified_listeners:
callback()

def addHasBeenModifiedListener(self, callback:FunctionType):
def addHasBeenModifiedListener(self, callback: FunctionType):
""" Add a callback that will trigger when the scene has been modified. """
self._has_been_modified_listeners.append(callback)

Expand Down Expand Up @@ -112,12 +114,12 @@ def drawGrid(self, painter: QPainter, rect: QRectF):
painter.setPen(pen)
painter.drawLines(*lines_light)

def save(self, filepath:str):
def save(self, filepath: str):
""" Save the scene into filepath. """
self.save_to_ipyg(filepath)
self.has_been_modified = False

def save_to_ipyg(self, filepath:str):
def save_to_ipyg(self, filepath: str):
""" Save the scene into filepath as interactive python graph (.ipyg). """
if '.' not in filepath:
filepath += '.ipyg'
Expand All @@ -129,7 +131,7 @@ def save_to_ipyg(self, filepath:str):
with open(filepath, 'w', encoding='utf-8') as file:
file.write(json.dumps(self.serialize(), indent=4))

def load(self, filepath:str):
def load(self, filepath: str):
""" Load a saved scene.

Args:
Expand All @@ -145,7 +147,7 @@ def load(self, filepath:str):
self.history.checkpoint("Loaded scene")
self.has_been_modified = False

def load_from_ipyg(self, filepath:str):
def load_from_ipyg(self, filepath: str):
""" Load an interactive python graph (.ipyg) into the scene.

Args:
Expand Down Expand Up @@ -177,23 +179,42 @@ def serialize(self) -> OrderedDict:
('edges', [edge.serialize() for edge in edges]),
])

def deserialize(self, data: OrderedDict, hashmap:dict=None, restore_id=True):
def create_block_from_file(
self, filepath: str, x: float = 0, y: float = 0):
""" Create a new block from a .ocbb file """
with open(filepath, 'r', encoding='utf-8') as file:
data = json.loads(file.read())
data["position"] = [x, y]
data["sockets"] = {}
self.create_block(data, None, False)

def create_block(self, data: OrderedDict, hashmap: dict = None,
restore_id: bool = True) -> OCBBlock:
""" Create a new block from an OrderedDict """

block = None
if data['block_type'] == 'base':
block = OCBBlock()
elif data['block_type'] == 'code':
block = OCBCodeBlock()
else:
raise NotImplementedError()
block.deserialize(data, hashmap, restore_id)
self.addItem(block)
if hashmap is not None:
hashmap.update({data['id']: block})
return block

def deserialize(self, data: OrderedDict,
hashmap: dict = None, restore_id: bool = True):
self.clear()
hashmap = hashmap if hashmap is not None else {}
if restore_id:
self.id = data['id']

# Create blocks
for block_data in data['blocks']:
if block_data['block_type'] == 'base':
block = OCBBlock()
elif block_data['block_type'] == 'code':
block = OCBCodeBlock()
else:
raise NotImplementedError()
block.deserialize(block_data, hashmap, restore_id)
self.addItem(block)
hashmap.update({block_data['id']: block})
self.create_block(block_data, hashmap, restore_id)

# Create edges
for edge_data in data['edges']:
Expand Down
Loading