Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: render the widgets in notebook #545

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sphinx:
configuration: docs/source/conf.py
builder: html
# Let the build fail if there are any warnings
fail_on_warning: true
#fail_on_warning: true

# Optional but recommended, declare the Python requirements required
# to build your documentation
Expand Down
22 changes: 22 additions & 0 deletions aiidalab_widgets_base/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,25 @@ def ase2spglib(ase_structure: Atoms) -> Tuple[Any, Any, Any]:
numbers = ase_structure.get_atomic_numbers()

return (lattice, positions, numbers)


# Load the temp profile for notebooks on readthedocs
def load_temp_profile():
"""Load the temp profile to make sure the docs build succeed even if the current
default profile of the AiiDA installation is not configured.
"""
from aiida import load_profile
from aiida.manage.configuration import get_config
from aiida.storage.sqlite_temp import SqliteTempBackend

profile = load_profile(
SqliteTempBackend.create_profile(
"readthedocs",
options={"warnings.development_version": False, "runner.poll.interval": 1},
debug=False,
),
allow_switch=True,
)
config = get_config()
config.add_profile(profile)
config.set_default_profile(profile.name)
8 changes: 4 additions & 4 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ def __init__(self, parameter, downloadable=True, **kwargs):
</style>
"""

pd.set_option("max_colwidth", 40)
pd.set_option("max_colwidth", 100)
dataf = pd.DataFrame(
[(key, value) for key, value in sorted(parameter.get_dict().items())],
columns=["Key", "Value"],
)
self.value += dataf.to_html(
classes="df", index=False
) # specify that exported table belongs to 'df' class
# specify that exported table belongs to 'df' class
# this is used to setup table's appearance using CSS
# `colwidths-auto` and `table` are also required for proper table rendering in MyST: https://myst-parser.readthedocs.io/en/latest/syntax/tables.html
self.value += dataf.to_html(classes="df colwidths-auto table", index=False)
if downloadable:
payload = base64.b64encode(dataf.to_csv(index=False).encode()).decode()
fname = f"{parameter.pk}.csv"
Expand Down
46 changes: 42 additions & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,38 @@
import time
from pathlib import Path

# Load the dummy profile to make sure the docs build succeed even if the current

# Load the temp profile to make sure the docs build succeed even if the current
# default profile of the AiiDA installation is not configured.
from aiida.manage.configuration import load_documentation_profile
def load_temp_profile():
"""Load the temp profile to make sure the docs build succeed even if the current
default profile of the AiiDA installation is not configured.
"""
from aiida import load_profile
from aiida.manage.configuration import get_config
from aiida.storage.sqlite_temp import SqliteTempBackend

profile = load_profile(
SqliteTempBackend.create_profile(
"readthedocs",
options={"warnings.development_version": False, "runner.poll.interval": 1},
debug=False,
),
allow_switch=True,
)
config = get_config()
config.add_profile(profile)
config.set_default_profile(profile.name)


load_documentation_profile()
load_temp_profile()

from aiidalab_widgets_base import __version__ # pylint: disable=wrong-import-position

# work around unpickleable functions
sys.path.append(str(Path(__file__).parent))
from ipywidgets_docs_utils import jupyterlab_markdown_heading

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
Expand All @@ -27,7 +51,21 @@
"myst_nb",
]

nb_execution_mode = "off"
myst_heading_anchors = 4
myst_heading_slug_func = jupyterlab_markdown_heading

nb_ipywidgets_js = {
"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js": {
"integrity": "sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=",
"crossorigin": "anonymous",
},
"https://cdn.jsdelivr.net/npm/@jupyter-widgets/html-manager@*/dist/embed-amd.js": {
"data-jupyter-widgets-cdn": "https://cdn.jsdelivr.net/npm/",
"crossorigin": "anonymous",
},
}

nb_execution_mode = "cache"

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
Expand Down
7 changes: 7 additions & 0 deletions docs/source/ipywidgets_docs_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def jupyterlab_markdown_heading(heading_text):
"""Use JupyterLab-style anchors for a consistent authoring/viewing experience.

This _must_ be defined in an external file as local functions can't be asssigned
to config values, as they can't be pickled.
"""
return heading_text.replace(" ", "-")
4 changes: 2 additions & 2 deletions notebooks/computational_resources.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_profile();"
"load_temp_profile()"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions notebooks/eln_configure.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_profile();"
"load_temp_profile()"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions notebooks/eln_import.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_profile();"
"load_temp_profile()"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions notebooks/process.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_profile();"
"load_temp_profile()"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions notebooks/process_list.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_profile();"
"load_temp_profile()"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions notebooks/structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_profile();"
"load_temp_profile()"
]
},
{
Expand Down Expand Up @@ -90,7 +90,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
19 changes: 15 additions & 4 deletions notebooks/viewers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
"metadata": {},
"outputs": [],
"source": [
"# Load the default AiiDA profile.\n",
"from aiida import orm, load_profile\n",
"load_profile();"
"# default profile of the AiiDA installation is not configured.\n",
"from aiida import orm\n",
"\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_temp_profile()"
]
},
{
Expand Down Expand Up @@ -46,6 +49,7 @@
" 'parameter 4' : [1, 2, 3],\n",
"})\n",
"vwr = viewer(p.store(), downloadable=True)\n",
"vwr.layout.width = '80%'\n",
"display(vwr)"
]
},
Expand Down Expand Up @@ -384,6 +388,13 @@
"i_node = orm.Int(1)\n",
"print(i_node.node_type)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -402,7 +413,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
16 changes: 12 additions & 4 deletions notebooks/wizard_apps.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "b5256919",
"id": "edfc9c5a",
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"from aiidalab_widgets_base.utils import load_temp_profile\n",
"\n",
"load_profile();"
"load_temp_profile()"
]
},
{
Expand Down Expand Up @@ -323,6 +323,14 @@
"# Display the app to the user.\n",
"display(app)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4672a973",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -341,7 +349,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
Loading