-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9df743e
commit 6950e78
Showing
7 changed files
with
171 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"color": "red" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "e0d2c1f7-3a10-4f30-a4d1-2d446de06487", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import stormvogel.layout\n", | ||
"from stormvogel.layout import DEFAULT" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "8c98f637-ed9c-430b-b414-09c665b307f8", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{'color': 'red'}\n", | ||
"{'color': 'red'}\n", | ||
"{'color': 'blue'}\n", | ||
"{'color': 'blue'}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Import custom layout\n", | ||
"l1 = stormvogel.layout.Layout(custom=True, custom_path=\"custom_layout.json\")\n", | ||
"l2 = stormvogel.layout.Layout(custom=True, custom_path=\"/home/ivo/git/stormvogel/notebooks/custom_layout.json\", custom_path_relative=False)\n", | ||
"# Import template layout\n", | ||
"l3 = stormvogel.layout.Layout(custom=False, template_path=\"layouts/default.json\")\n", | ||
"l4 = stormvogel.layout.Layout(custom=False, template_path=DEFAULT)\n", | ||
"l5 = " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9198a4b4-12cd-47a4-8950-278c18023192", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Contains the code responsible for saving/loading layouts and modifying them interactively.""" | ||
|
||
from pyvis.network import Network | ||
import os | ||
import json | ||
|
||
DEFAULT = "layouts/default.json" | ||
|
||
|
||
class Layout: | ||
layout: dict | ||
|
||
def __init__( | ||
self, | ||
custom: bool, | ||
path: str | None = None, | ||
path_relative: bool = True, | ||
template_path: str = DEFAULT, | ||
) -> None: | ||
"""Load a new Layout from a json file. Use either a custom or a template file. | ||
Args: | ||
custom (bool, optional): If set to true, stormvogel will look for your custom layout.json file. Otherwise a template will be used. | ||
path (str, optional): Relavant if custom is true. Path to your custom layout file, relative to the current working directory. Defaults to None. | ||
path_relative (bool): Relavant if custom is true. If set to true, then stormvogel will look for a custom layout file relative to the current working directory. | ||
template_path (str, optional): Relavant if custom is false. Path to a template layout files. | ||
These are stored in the folder layouts. For simplicity, we recommed using the constants DEFAULT, etc. | ||
Defaults to DEFAULT (="layouts/default.json"). | ||
""" | ||
if custom: | ||
if path is None: | ||
raise Exception( | ||
"If custom is set to true, then the path needs to be set." | ||
) | ||
cwd = os.getcwd() | ||
if path_relative: | ||
complete_path = os.path.join(cwd, path) | ||
else: | ||
complete_path = path | ||
with open(complete_path) as f: | ||
json_string = f.read() | ||
self.layout = json.loads(json_string) | ||
else: | ||
package_root_dir = os.path.dirname(os.path.realpath(__file__)) | ||
with open(os.path.join(package_root_dir, template_path)) as f: | ||
json_string = f.read() | ||
self.layout = json.loads(json_string) | ||
|
||
def set_nt_layout(self, nt: Network) -> None: | ||
"""Set the layout of the network passed as the arugment.""" | ||
# We here use <> instead of {} because the f-string formatting already uses them. | ||
option_string = f""" | ||
var options = < | ||
"nodes": < | ||
"color": < | ||
"background": "{self.layout["color"]}", | ||
"border": "black" | ||
> | ||
> | ||
>""".replace("<", "{").replace(">", "}") | ||
print(option_string) | ||
nt.set_options(option_string) | ||
|
||
def save(self) -> None: | ||
raise NotImplementedError() | ||
|
||
def show_buttons(self) -> None: | ||
raise NotImplementedError() | ||
|
||
def __str__(self) -> str: | ||
raise NotImplementedError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"color": "blue" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters