-
Notifications
You must be signed in to change notification settings - Fork 149
Add optional built-in guided tour to webgl viewers #660
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
Open
jackgallant
wants to merge
5
commits into
gallantlab:main
Choose a base branch
from
jackgallant:claude/webgl-tour
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
543b992
Add self-contained tour.js for optional webgl tour
jackgallant 1562f0e
Add tour.css for optional webgl tour
jackgallant 421ba9c
Conditionally include tour assets in webgl template + test
jackgallant 942c2fa
Add tour= option to make_static (viewopts.tour + template flag)
jackgallant cff92cf
Auto-construct the built-in tour and bind the 'o' toggle
jackgallant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,31 @@ | ||
| """Tests for the optional guided-tour feature (cortex.webgl.make_static(tour=True)).""" | ||
| import cortex.webgl # noqa: F401 (ensures cortex.webgl is importable) | ||
| from cortex.webgl import serve | ||
| from cortex.webgl.FallbackLoader import FallbackLoader | ||
|
|
||
|
|
||
| def _render_base_template(tour): | ||
| """Render the base webgl template with the given `tour` flag (no subject needed).""" | ||
| loader = FallbackLoader([serve.cwd]) | ||
| tpl = loader.load("template.html") | ||
| html = tpl.generate( | ||
| title="test", | ||
| leapmotion=False, | ||
| python_interface=False, | ||
| tour=tour, | ||
| colormaps=[("RdBu_r", "colormaps/RdBu_r.png")], | ||
| default_cmap="RdBu_r", | ||
| ) | ||
| return html.decode("utf-8") if isinstance(html, bytes) else html | ||
|
|
||
|
|
||
| def test_tour_include_present_when_enabled(): | ||
| html = _render_base_template(True) | ||
| assert "resources/js/tour.js" in html | ||
| assert "resources/css/tour.css" in html | ||
|
|
||
|
|
||
| def test_tour_include_absent_by_default(): | ||
| html = _render_base_template(False) | ||
| assert "resources/js/tour.js" not in html | ||
| assert "resources/css/tour.css" not in html |
This file contains hidden or 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,35 @@ | ||
| /* tour.css — styles for the optional guided-tour box (see resources/js/tour.js). | ||
| Adapted from the gallantlab semantic viewers (after the NYT visualizer). */ | ||
| div#tourbox { | ||
| font-family: Helvetica, Arial, sans-serif; | ||
| color: white; | ||
| width: 25%; | ||
| left: 0; | ||
| min-width: 600px; | ||
| z-index: 100; | ||
| visibility: visible; | ||
| position: absolute; | ||
| top: 0; | ||
| opacity: 0.95; | ||
| background-color: rgba(0,0,0,0.7); | ||
| padding: 0 10px 10px 10px; | ||
| } | ||
| div.tour-stepper { margin-bottom: 10px; } | ||
| div.tour-steps { margin-bottom: 5px; display: inline-block; } | ||
| div.tour-step { | ||
| display: inline-block; width: 6px; height: 6px; margin-right: 10px; | ||
| border-radius: 4px; border: 1px solid white; cursor: pointer; | ||
| background: none; transition: background 0.5s; | ||
| } | ||
| div.tour-steps .active { background: white; } | ||
| div.tour-hideshow { display: inline-block; font-size: 8pt; cursor: pointer; } | ||
| div.tour-button { | ||
| border: 1px solid white; color: #cccccc; width: 100px; padding: 10px 0; | ||
| display: inline-block; background: none; text-align: center; opacity: 0.6; | ||
| transition: background 0.5s; | ||
| } | ||
| div.tour-button.active { opacity: 1.0; cursor: pointer; } | ||
| div.tour-back { border-top-left-radius: 5px; border-bottom-left-radius: 5px; } | ||
| div.tour-next { border-top-right-radius: 5px; border-bottom-right-radius: 5px; } | ||
| div.tour-title { font-size: 18pt; margin-bottom: 10px; } | ||
| div.tour-content { font-size: 12pt; font-family: Helvetica, Arial, sans-serif; } |
This file contains hidden or 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 hidden or 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,113 @@ | ||
| /* tour.js — optional guided-tour stepper for pycortex webgl viewers. | ||
| * | ||
| * Enabled by cortex.webgl.make_static(..., tour=True). Ships a generic stub | ||
| * tour (no dataset-specific text); mriview.js constructs it on boot when | ||
| * viewopts.tour is set and binds the 'o' key to Tour.toggle(). | ||
| * | ||
| * Adapted from the tour used in the gallantlab semantic viewers (itself after | ||
| * the NYT visualizer by Gregor Aisch & Amanda Cox). Cleaned for reuse: it | ||
| * self-injects its markup, per-step `view`/`call` hooks are optional, and its | ||
| * keyboard show/hide acts on the whole box (the old showhide left a stray strip). | ||
| */ | ||
| (function () { | ||
| "use strict"; | ||
|
|
||
| // self-contained tourbox markup (no template HTML needed) | ||
| var TOURBOX_HTML = | ||
| '<div class="tour-stepper">' + | ||
| '<div class="tour-steps"></div>' + | ||
| '<div class="tour-hideshow">(hide tour)</div><br/>' + | ||
| '<div class="tour-button tour-back">Back</div>' + | ||
| '<div class="tour-button tour-next active">Next</div>' + | ||
| '</div>' + | ||
| '<div class="tour-title"></div>' + | ||
| '<div class="tour-content"></div>'; | ||
|
|
||
| // generic stub steps — nothing dataset-specific (f/i/r/h/o are real core keys) | ||
| var STEPS = [ | ||
| {title: "Welcome", | ||
| content: "<p>This is a short guided tour of the viewer. Use <b>Back</b> / <b>Next</b> or the dots above to step through it. Press <b>o</b> to hide or show this box.</p>"}, | ||
| {title: "Navigating", | ||
| content: "<p>Drag to rotate the brain, scroll to zoom, and <b>Shift</b>+drag to pan.</p>"}, | ||
| {title: "The surface", | ||
| content: "<p>Press <b>f</b> to flatten the cortex, <b>i</b> to inflate it, and <b>r</b> to reset the view.</p>"}, | ||
| {title: "Explore", | ||
| content: "<p>Press <b>h</b> at any time to see every keyboard shortcut. That's it — go explore!</p>"} | ||
| ]; | ||
|
|
||
| // Tour(viewer, content?) — content defaults to the generic stub STEPS. | ||
| var Tour = function (viewer, content) { | ||
| this.viewer = viewer; | ||
| this.content = content || STEPS; | ||
| this.hidden = false; | ||
| this.object = document.createElement("div"); | ||
| this.object.id = "tourbox"; | ||
| this.object.innerHTML = TOURBOX_HTML; | ||
| document.body.appendChild(this.object); | ||
| this.setup(); | ||
| }; | ||
|
|
||
| Tour.prototype.setup = function () { | ||
| var self = this; | ||
| $(this.object).find(".tour-hideshow").click(this.showhide.bind(this)); | ||
| this.content.forEach(function (step, i) { | ||
| var el = document.createElement("div"); | ||
| $(el).addClass("tour-step").attr("title", step.title) | ||
| .click(function () { self.goto_step(i); }); | ||
| $(self.object).find(".tour-steps").append(el); | ||
| }); | ||
| this.goto_step(0); | ||
| }; | ||
|
|
||
| Tour.prototype.goto_step = function (idx) { | ||
| this.current_step = parseInt(idx, 10); | ||
| this.update_buttons(); | ||
| var steps = $(this.object).find(".tour-step"); | ||
| steps.removeClass("active"); | ||
| $(steps[this.current_step]).addClass("active"); | ||
| var cont = this.content[this.current_step]; | ||
| $(this.object).find(".tour-title").html(cont.title); | ||
| $(this.object).find(".tour-content").html(cont.content); | ||
| // per-step camera move / callback are optional (the stub has neither) | ||
| if (cont.view && this.viewer && this.viewer.animate) this.viewer.animate(cont.view); | ||
| if (typeof cont.call === "function") cont.call(this.viewer); | ||
| }; | ||
|
|
||
| Tour.prototype.update_buttons = function () { | ||
| var self = this; | ||
| var last_idx = this.current_step - 1; | ||
| var next_idx = this.current_step + 1; | ||
| var back = $(this.object).find(".tour-back"); | ||
| var next = $(this.object).find(".tour-next"); | ||
| back.unbind("click"); | ||
| if (this.current_step === 0) { | ||
| back.removeClass("active"); | ||
| } else { | ||
| back.addClass("active").click(function () { self.goto_step(last_idx); }); | ||
| } | ||
| if (this.current_step === this.content.length - 1) next_idx = 0; | ||
| next.unbind("click").click(function () { self.goto_step(next_idx); }); | ||
| }; | ||
|
|
||
| // on-screen link: collapse to / expand from the stepper (keeps the affordance) | ||
| Tour.prototype.showhide = function () { | ||
| var box = $(this.object); | ||
| if (this.hidden) { | ||
| box.find(".tour-button, .tour-title, .tour-content").show(); | ||
| box.find(".tour-hideshow").html("(hide tour)"); | ||
| this.hidden = false; | ||
| } else { | ||
| box.find(".tour-button, .tour-title, .tour-content").hide(); | ||
| box.find(".tour-hideshow").html("(show tour)"); | ||
| this.hidden = true; | ||
| } | ||
| }; | ||
|
|
||
| // keyboard 'o': fully show/hide the whole box (nothing left behind — the fix) | ||
| Tour.prototype.toggle = function () { | ||
| var el = this.object; | ||
| el.style.display = (el.style.display === "none" ? "" : "none"); | ||
| }; | ||
|
|
||
| window.Tour = Tour; | ||
| })(); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rendering this template via
cortex.webgl.show()(which usesmixer.htmlextendingtemplate.html) will crash with aNameError: name 'tour' is not definedbecausetouris not passed tohtml.generate()inshow()'sMixerHandler.To prevent this crash and ensure backward compatibility with other viewer entry points, wrap the
tourconditional block in atry/except NameErrorblock. This is a clean, template-level solution that avoids breakingcortex.webgl.show().