Skip to content

Commit

Permalink
Handle case with the source files being in folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sofiia-chorna committed Jun 3, 2024
1 parent 6b54181 commit f4903c6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
6 changes: 2 additions & 4 deletions python/chemiscope/sphinx/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class Chemiscope(Directive):
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = {
"filename": str,
"mode": str,
}
option_spec = {"filename": str, "mode": str, "gallery_dirs": str}

def run(self):
node = chemiscope()
node["filename"] = self.options.get("filename")
node["mode"] = self.options.get("mode")
node["gallery_dirs"] = self.options.get("gallery_dirs")
self.state.nested_parse(self.content, self.content_offset, node)
return [node]
7 changes: 5 additions & 2 deletions python/chemiscope/sphinx/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ def depart_chemiscope_latex(self, node):


def visit_chemiscope_html(self, node):
self.body.append(generate_html_content(node["filename"], node["mode"]))
self.body.append(
generate_html_content(node["filename"], node["gallery_dirs"], node["mode"])
)


def depart_chemiscope_html(self, node):
pass


def generate_html_content(filename, mode="default"):
def generate_html_content(filename, gallery_dirs, mode="default"):
# Generate a unique id for the chemiscope div
div_id = f"sphinx-gallery-{uuid.uuid4()}"

Expand All @@ -41,4 +43,5 @@ def generate_html_content(filename, mode="default"):
html_template.replace("{{div_id}}", div_id)
.replace("{{filename}}", filename)
.replace("{{mode}}", mode)
.replace("{{gallery_dirs}}", gallery_dirs)
)
4 changes: 3 additions & 1 deletion python/chemiscope/sphinx/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ def __init__(self, examples_dir):
def __repr__(self):
return "ChemiscopeScraper"

def __call__(self, _block, block_vars, _gallery_conf):
def __call__(self, _block, block_vars, gallery_conf):
# Retrieve the chemiscope widget from block variables
widget = block_vars.get("example_globals", {}).get("___")
mode = self.get_widget_mode(widget)

if mode is not None:
dataset_file_path = self.iterator.next()
widget.save(dataset_file_path)
gallery_dirs = gallery_conf.get("gallery_dirs")

return f""".. chemiscope::
:filename: {os.path.basename(dataset_file_path)}
:mode: {mode}
:gallery_dirs: {gallery_dirs}
"""
else:
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.0.4/pako.min.js"></script>

<!-- JS scripts -->
<script src="static/js/chemiscope.min.js"></script>
<script src="static/js/chemiscope-sphinx-gallery.js"></script>
<script src="/build/html/{{gallery_dirs}}/static/js/chemiscope.min.js"></script>
<script src="/build/html/{{gallery_dirs}}/static/js/chemiscope-sphinx-gallery.js"></script>

<!-- CSS styles -->
<link rel="stylesheet" href="static/css/chemiscope-sphinx-gallery.css" type="text/css" />
<link rel="stylesheet" href="/build/html/{{gallery_dirs}}/static/css/chemiscope-sphinx-gallery.css" type="text/css" />

<!-- Warning Display -->
<div id="{{div_id}}-warning-display" class="chemiscope-sphinx-gallery-warning">
Expand All @@ -17,7 +17,7 @@

<!-- Loading Spinner -->
<div id="{{div_id}}-loading" class="chemiscope-sphinx-gallery-spinner">
<img src="static/loading-icon.svg" alt="Loading icon" />
<img src="/build/html/{{gallery_dirs}}/static/loading-icon.svg" alt="Loading icon" />
</div>

<!-- Chemiscope Visualization -->
Expand Down

0 comments on commit f4903c6

Please sign in to comment.