Skip to content

Commit

Permalink
Format notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas committed Jul 29, 2024
1 parent 6965c76 commit 7fb3e2a
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 66 deletions.
41 changes: 25 additions & 16 deletions delete.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"load_profile()\n",
"\n",
"# Parse the primary key from the Jupyter notebook URL\n",
"url = urlparse.urlsplit(jupyter_notebook_url) # noqa F821\n",
"url = urlparse.urlsplit(jupyter_notebook_url) # noqa F821\n",
"query = urlparse.parse_qs(url.query)\n",
"pk = int(query['pk'][0])\n",
"pk = int(query[\"pk\"][0])\n",
"\n",
"\n",
"def display_node_details(pk):\n",
Expand All @@ -51,31 +51,36 @@
" if dry_run:\n",
" _, was_deleted = delete_nodes([pk], dry_run=True)\n",
" if was_deleted:\n",
" print(f'Dry run: Node {pk} can be deleted.')\n",
" print(f\"Dry run: Node {pk} can be deleted.\")\n",
" return\n",
" \n",
"\n",
" _, was_deleted = delete_nodes([pk], dry_run=False)\n",
" if was_deleted:\n",
" print(f'Node {pk} deleted successfully.')\n",
" print(f\"Node {pk} deleted successfully.\")\n",
"\n",
"\n",
"def confirm_deletion(_):\n",
" if delete_confirmation.value.lower() in ('y', 'yes'):\n",
" if delete_confirmation.value.lower() in (\"y\", \"yes\"):\n",
" delete_node(pk, dry_run=False)\n",
" else:\n",
" print('Deletion aborted.')\n",
" print(\"Deletion aborted.\")\n",
"\n",
"\n",
"def find_linked_qeapp_jobs(root_node_pk, process_label='QeAppWorkChain'):\n",
"def find_linked_qeapp_jobs(root_node_pk, process_label=\"QeAppWorkChain\"):\n",
" \"\"\"Query all linked node with process_label = QeAppWorkChain.\"\"\"\n",
" from aiida.orm import Node, QueryBuilder\n",
" from aiida.orm.nodes.process.workflow.workchain import WorkChainNode\n",
"\n",
" qb = QueryBuilder()\n",
" qb.append(WorkChainNode, filters={'id': root_node_pk}, tag='root')\n",
" qb.append(Node, with_incoming='root', tag='calcjob')\n",
" qb.append(WorkChainNode, filters={\"id\": root_node_pk}, tag=\"root\")\n",
" qb.append(Node, with_incoming=\"root\", tag=\"calcjob\")\n",
" # There are seems a bug with `with_ancestors` in the QueryBuilder, so we have to use `with_incoming` instead.\n",
" # For the moment, it's safe to use `with_incoming` since we check it very time we delete a QEApp \n",
" qb.append(WorkChainNode, filters={'attributes.process_label': process_label}, with_incoming='calcjob')\n",
" # For the moment, it's safe to use `with_incoming` since we check it very time we delete a QEApp\n",
" qb.append(\n",
" WorkChainNode,\n",
" filters={\"attributes.process_label\": process_label},\n",
" with_incoming=\"calcjob\",\n",
" )\n",
" results = qb.all()\n",
" if len(results) == 0:\n",
" return None\n",
Expand All @@ -97,12 +102,16 @@
" else:\n",
" # Ask for confirmation\n",
" nodes, _ = delete_nodes([pk], dry_run=True)\n",
" display(Markdown(f'**YOU ARE ABOUT TO DELETE `{len(nodes)}` NODES! THIS CANNOT BE UNDONE!**'))\n",
" display(\n",
" Markdown(\n",
" f\"**YOU ARE ABOUT TO DELETE `{len(nodes)}` NODES! THIS CANNOT BE UNDONE!**\"\n",
" )\n",
" )\n",
" delete_confirmation = widgets.Text(\n",
" value='',\n",
" value=\"\",\n",
" placeholder='Type \"yes\" to confirm',\n",
" description='Confirm:',\n",
" disabled=False\n",
" description=\"Confirm:\",\n",
" disabled=False,\n",
" )\n",
" confirm_button = widgets.Button(description=\"Delete Node\")\n",
" confirm_button.on_click(confirm_deletion)\n",
Expand Down
183 changes: 136 additions & 47 deletions plugin_list.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
"# Get the current working directory\n",
"cwd = Path.cwd()\n",
"# Define a relative path\n",
"relative_path = 'plugins.yaml'\n",
"relative_path = \"plugins.yaml\"\n",
"# Resolve the relative path to an absolute path\n",
"yaml_file = cwd / relative_path\n",
"\n",
"# Load the YAML content\n",
"with yaml_file.open('r') as file:\n",
"with yaml_file.open(\"r\") as file:\n",
" data = yaml.safe_load(file)"
]
},
Expand All @@ -54,7 +54,8 @@
"\n",
"def is_package_installed(package_name):\n",
" import importlib\n",
" package_name = package_name.replace('-', '_')\n",
"\n",
" package_name = package_name.replace(\"-\", \"_\")\n",
" try:\n",
" importlib.import_module(package_name)\n",
" except ImportError:\n",
Expand All @@ -67,16 +68,20 @@
" \"\"\"Reads output from the process and forwards it to the output widget.\"\"\"\n",
" while True:\n",
" output = process.stdout.readline()\n",
" if process.poll() is not None and output == '':\n",
" if process.poll() is not None and output == \"\":\n",
" break\n",
" if output:\n",
" output_widget.value += f\"\"\"<div style=\"background-color: #3B3B3B; color: #FFFFFF;\">{output}</div>\"\"\"\n",
"\n",
"\n",
"def execute_command_with_output(command, output_widget, install_btn, remove_btn, action=\"install\"):\n",
"def execute_command_with_output(\n",
" command, output_widget, install_btn, remove_btn, action=\"install\"\n",
"):\n",
" \"\"\"Execute a command and stream its output to the given output widget.\"\"\"\n",
" output_widget.value = \"\" # Clear the widget\n",
" process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)\n",
" process = subprocess.Popen(\n",
" command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1\n",
" )\n",
" # Create a thread to read the output stream and write it to the output widget\n",
" thread = Thread(target=stream_output, args=(process, output_widget))\n",
" thread.start()\n",
Expand All @@ -95,30 +100,51 @@
" return False\n",
"\n",
"\n",
"def install_package(package_name, pip, github, post_install, output_container, message_container, install_btn, remove_btn, accordion, index):\n",
"def install_package(\n",
" package_name,\n",
" pip,\n",
" github,\n",
" post_install,\n",
" output_container,\n",
" message_container,\n",
" install_btn,\n",
" remove_btn,\n",
" accordion,\n",
" index,\n",
"):\n",
" if pip:\n",
" command = [\"pip\", \"install\", pip, \"--user\"]\n",
" else:\n",
" command = [\"pip\", \"install\", \"git+\" + github, \"--user\"]\n",
" message_container.value = f\"\"\"<div style=\"color: #000000;\">Installing {package_name}...</div>\"\"\"\n",
" result = execute_command_with_output(command, output_container, install_btn, remove_btn)\n",
" message_container.value = (\n",
" f\"\"\"<div style=\"color: #000000;\">Installing {package_name}...</div>\"\"\"\n",
" )\n",
" result = execute_command_with_output(\n",
" command, output_container, install_btn, remove_btn\n",
" )\n",
" # Execute post install if defined in the plugin.yaml:\n",
" if post_install: \n",
" message_container.value += \"\"\"<div style=\"color: #008000;\">Post installation step...</div>\"\"\"\n",
" command = [sys.executable, '-m', package_name.replace('-','_'), post_install]\n",
" if post_install:\n",
" message_container.value += (\n",
" \"\"\"<div style=\"color: #008000;\">Post installation step...</div>\"\"\"\n",
" )\n",
" command = [sys.executable, \"-m\", package_name.replace(\"-\", \"_\"), post_install]\n",
" # Execute the command\n",
" result = subprocess.run(command, capture_output=True, text=True, check=False)\n",
" # if the package was installed successfully\n",
" if result:\n",
" message_container.value += \"\"\"<div style=\"color: #008000;\">Initiating test to load the plugin...</div>\"\"\"\n",
" # Test plugin functionality\n",
" command = [sys.executable, '-m', 'aiidalab_qe', 'test-plugin', package_name]\n",
" command = [sys.executable, \"-m\", \"aiidalab_qe\", \"test-plugin\", package_name]\n",
" # Execute the command\n",
" result = subprocess.run(command, capture_output=True, text=True, check=False)\n",
" if result.returncode == 0:\n",
" # restart daemon\n",
" message_container.value = \"\"\"<div style=\"color: #008000;\">Loading plugin test passed.</div>\"\"\"\n",
" message_container.value += \"\"\"<div style=\"color: #008000;\">Plugin installed successfully.</div>\"\"\"\n",
" message_container.value = (\n",
" \"\"\"<div style=\"color: #008000;\">Loading plugin test passed.</div>\"\"\"\n",
" )\n",
" message_container.value += (\n",
" \"\"\"<div style=\"color: #008000;\">Plugin installed successfully.</div>\"\"\"\n",
" )\n",
" accordion.set_title(index, f\"{accordion.get_title(index)[:-2]} ✅\")\n",
" command = [\"verdi\", \"daemon\", \"restart\"]\n",
" subprocess.run(command, capture_output=True, shell=False, check=False)\n",
Expand All @@ -127,42 +153,76 @@
" message_container.value = f\"\"\"<div style=\"color: #FF0000;\">The plugin '{package_name}' was installed successfully but plugin functionality test failed: {result.stderr}. </div>\"\"\"\n",
" message_container.value += \"\"\"<div style=\"color: #FF0000;\">This may be due to compatibility issues with the current AiiDAlab QEApp version. Please contact the plugin author for further assistance.</div>\"\"\"\n",
" message_container.value += \"\"\"<div style=\"color: #FF0000;\">To prevent potential issues, the plugin will now be uninstalled.</div>\"\"\"\n",
" remove_package(package_name, output_container, message_container, install_btn, remove_btn, accordion, index)\n",
" remove_package(\n",
" package_name,\n",
" output_container,\n",
" message_container,\n",
" install_btn,\n",
" remove_btn,\n",
" accordion,\n",
" index,\n",
" )\n",
"\n",
"\n",
"def remove_package(package_name, output_container, message_container, install_btn, remove_btn, accordion, index):\n",
" message_container.value += f\"\"\"<div style=\"color: #FF0000;\">Removing {package_name}...</div>\"\"\"\n",
" package_name = package_name.replace('-', '_')\n",
"def remove_package(\n",
" package_name,\n",
" output_container,\n",
" message_container,\n",
" install_btn,\n",
" remove_btn,\n",
" accordion,\n",
" index,\n",
"):\n",
" message_container.value += (\n",
" f\"\"\"<div style=\"color: #FF0000;\">Removing {package_name}...</div>\"\"\"\n",
" )\n",
" package_name = package_name.replace(\"-\", \"_\")\n",
" command = [\"pip\", \"uninstall\", \"-y\", package_name]\n",
" result = execute_command_with_output(command, output_container, install_btn, remove_btn, action=\"remove\")\n",
" result = execute_command_with_output(\n",
" command, output_container, install_btn, remove_btn, action=\"remove\"\n",
" )\n",
" if result:\n",
" message_container.value += f\"\"\"<div style=\"color: #008000;\">{package_name} removed successfully.</div>\"\"\"\n",
" accordion.set_title(index, f\"{accordion.get_title(index)[:-2]} ☐\")\n",
" command = [\"verdi\", \"daemon\", \"restart\"]\n",
" subprocess.run(command, capture_output=True, shell=False, check=False)\n",
"\n",
"\n",
"def run_remove_button(package_name, output_container, message_container, install_btn, remove_btn, accordion, index):\n",
"def run_remove_button(\n",
" package_name,\n",
" output_container,\n",
" message_container,\n",
" install_btn,\n",
" remove_btn,\n",
" accordion,\n",
" index,\n",
"):\n",
" message_container.value = \"\"\n",
" remove_package(package_name, output_container, message_container, install_btn, remove_btn, accordion, index)\n",
" remove_package(\n",
" package_name,\n",
" output_container,\n",
" message_container,\n",
" install_btn,\n",
" remove_btn,\n",
" accordion,\n",
" index,\n",
" )\n",
"\n",
"\n",
"accordion = ipw.Accordion()\n",
"\n",
"for i, (plugin_name, plugin_data) in enumerate(data.items()):\n",
" installed = is_package_installed(plugin_name)\n",
" \n",
"\n",
" # Output container with customized styling\n",
" output_container = ipw.HTML(\n",
" value=\"\"\"\n",
" <div style=\"background-color: #3B3B3B; color: #FFFFFF; height: 100%; overflow: auto;\">\n",
" </div>\n",
" \"\"\",\n",
" layout=ipw.Layout(\n",
" max_height='250px', \n",
" overflow='auto',\n",
" border='2px solid #CCCCCC'\n",
" )\n",
" max_height=\"250px\", overflow=\"auto\", border=\"2px solid #CCCCCC\"\n",
" ),\n",
" )\n",
" # Output container with customized styling\n",
" message_container = ipw.HTML(\n",
Expand All @@ -171,31 +231,60 @@
" </div>\n",
" \"\"\",\n",
" layout=ipw.Layout(\n",
" max_height='250px', \n",
" overflow='auto',\n",
" border='2px solid #CCCCCC'\n",
" )\n",
" max_height=\"250px\", overflow=\"auto\", border=\"2px solid #CCCCCC\"\n",
" ),\n",
" )\n",
"\n",
" details = (\n",
" f\"Author: {plugin_data.get('author', 'N/A')}<br>\"\n",
" f\"Description: {plugin_data.get('description', 'No description available')}<br>\"\n",
" )\n",
" \n",
" details = f\"Author: {plugin_data.get('author', 'N/A')}<br>\" \\\n",
" f\"Description: {plugin_data.get('description', 'No description available')}<br>\"\n",
" if 'documentation' in plugin_data:\n",
" if \"documentation\" in plugin_data:\n",
" details += f\"Documentation: <a href='{plugin_data['documentation']}' target='_blank'>Visit</a><br>\"\n",
" if 'github' in plugin_data:\n",
" details += f\"Github: <a href='{plugin_data.get('github')}' target='_blank'>Visit</a>\"\n",
" if \"github\" in plugin_data:\n",
" details += (\n",
" f\"Github: <a href='{plugin_data.get('github')}' target='_blank'>Visit</a>\"\n",
" )\n",
"\n",
" install_btn = ipw.Button(description=\"Install\", button_style='success', disabled=installed)\n",
" remove_btn = ipw.Button(description=\"Remove\", button_style='danger', disabled=not installed)\n",
" install_btn = ipw.Button(\n",
" description=\"Install\", button_style=\"success\", disabled=installed\n",
" )\n",
" remove_btn = ipw.Button(\n",
" description=\"Remove\", button_style=\"danger\", disabled=not installed\n",
" )\n",
"\n",
" install_btn.on_click(lambda _btn, pn=plugin_name, pip=plugin_data.get('pip', None), github=plugin_data.get('github', ''), post = plugin_data.get('post_install', None), oc=output_container, mc=message_container, ib=install_btn, rb=remove_btn, ac=accordion, index=i: install_package(pn, pip, github, post, oc, mc, ib, rb, ac, index)) # noqa: B008\n",
" remove_btn.on_click(lambda _btn, pn=plugin_name, oc=output_container, mc=message_container, ib=install_btn, rb=remove_btn, ac=accordion, index=i: run_remove_button(pn, oc, mc, ib, rb, ac, index))\n",
" install_btn.on_click(\n",
" lambda _btn,\n",
" pn=plugin_name,\n",
" pip=plugin_data.get(\"pip\", None), # noqa: B008\n",
" github=plugin_data.get(\"github\", \"\"), # noqa: B008\n",
" post=plugin_data.get(\"post_install\", None), # noqa: B008\n",
" oc=output_container,\n",
" mc=message_container,\n",
" ib=install_btn,\n",
" rb=remove_btn,\n",
" ac=accordion,\n",
" index=i: install_package(pn, pip, github, post, oc, mc, ib, rb, ac, index)\n",
" )\n",
" remove_btn.on_click(\n",
" lambda _btn,\n",
" pn=plugin_name,\n",
" oc=output_container,\n",
" mc=message_container,\n",
" ib=install_btn,\n",
" rb=remove_btn,\n",
" ac=accordion,\n",
" index=i: run_remove_button(pn, oc, mc, ib, rb, ac, index)\n",
" )\n",
"\n",
" box = ipw.VBox([\n",
" ipw.HTML(details),\n",
" ipw.HBox([install_btn, remove_btn]),\n",
" message_container,\n",
" output_container,\n",
" ])\n",
" box = ipw.VBox(\n",
" [\n",
" ipw.HTML(details),\n",
" ipw.HBox([install_btn, remove_btn]),\n",
" message_container,\n",
" output_container,\n",
" ]\n",
" )\n",
"\n",
" title_with_icon = f\"{plugin_name} {'✅' if installed else '☐'}\"\n",
" accordion.set_title(i, title_with_icon)\n",
Expand Down
6 changes: 3 additions & 3 deletions qe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@
" f'<p style=\"text-align:right;\">Copyright (c) {current_year} AiiDAlab team &#8195Version: {__version__}</p>'\n",
")\n",
"\n",
"url = urlparse.urlsplit(jupyter_notebook_url) # noqa F821\n",
"url = urlparse.urlsplit(jupyter_notebook_url) # noqa F821\n",
"query = urlparse.parse_qs(url.query)\n",
"\n",
"\n",
"app_with_work_chain_selector = App(qe_auto_setup=True)\n",
"# if a pk is provided in the query string, set it as the value of the work_chain_selector\n",
"if 'pk' in query:\n",
" pk = int(query['pk'][0])\n",
"if \"pk\" in query:\n",
" pk = int(query[\"pk\"][0])\n",
" app_with_work_chain_selector.work_chain_selector.value = pk\n",
"\n",
"output = ipw.Output()\n",
Expand Down

0 comments on commit 7fb3e2a

Please sign in to comment.