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

Add interactive plots with Plotly #163

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

javierbg
Copy link

@javierbg javierbg commented Nov 25, 2022

Description

This pull request adds a new module to the dashboard for showing interactive plots in the browser.

Motivation and Context

See issue #162

Checklist:

@javierbg javierbg requested review from a team as code owners November 25, 2022 11:34
@javierbg javierbg requested review from tcmoore3 and Charlottez112 and removed request for a team November 25, 2022 11:34
@javierbg
Copy link
Author

javierbg commented Nov 25, 2022

Do you consider that a new test is needed for the module?

Copy link
Member

@bdice bdice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javierbg Thanks for the pull request! The code looks solid but I haven't tested it yet. This seems like a great feature to include in the dashboard examples. Can you add an example there? Then I'll re-review and test the example on my machine.

changelog.txt Outdated Show resolved Hide resolved
doc/api.rst Outdated
@@ -45,6 +45,7 @@ Dashboard Modules
modules.StatepointList
modules.TextDisplay
modules.VideoViewer
modules.PlotViewer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetize this in the list.

@@ -20,4 +21,5 @@
"StatepointList",
"TextDisplay",
"VideoViewer",
"PlotViewer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetize this in the list.

signac_dashboard/modules/plot_viewer.py Outdated Show resolved Hide resolved
signac_dashboard/modules/plot_viewer.py Outdated Show resolved Hide resolved
signac_dashboard/modules/plot_viewer.py Outdated Show resolved Hide resolved
signac_dashboard/modules/plot_viewer.py Outdated Show resolved Hide resolved
dashboard.app.route("/module/plot_viewer/<path:filename>")(plot_viewer_asset)

# Register assets
assets = ["js/plot_viewer.js", "js/plotly-2.16.1.min.js"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This asset is pretty large and it seems like it changes often (could get outdated, at the relatively slow pace of signac-dashboard development). Can we use the CDN instead?

Note that you'll need to rewrite the git history on this branch to prevent the inclusion of this file in the git history. If you need help with this task or are uncertain about how to do it correctly, let me know and I'll take care of it!

Suggested change
assets = ["js/plot_viewer.js", "js/plotly-2.16.1.min.js"]
assets = ["js/plot_viewer.js", "https://cdn.plot.ly/plotly-2.16.1.min.js"]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I have never done it before but I can give it a try. I'll let you know if I need any help. It should not be a problem to commit your changes first, right? (by the way, is it ok if I batch the smaller ones?)

I'm happy to make this change, the asset is definitely very large, but maybe it's not so popular to have to rely on an external service for this module. Maybe a solution where the user can provide a local copy as a fallback would be useful? I guess it would be a niche situation and not work the effort, but I thought I'd mention it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit GitHub suggestions individually or batched. Your decision!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a solution where the user can provide a local copy as a fallback would be useful? I guess it would be a niche situation and not work the effort, but I thought I'd mention it.

Yeah, I don't think we are intending for signac-dashboard to be fully usable offline. The core features are baked in, but I don't think it's necessary to include large assets for specific modules like this one in the distributed package. It increases the size of the signac-dashboard repo and downloaded package, and has a high likelihood of going out of date.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javierbg to rewrite the history of the branch perform an "interactive rebase" and remove the commits that added the large file.

The dashboard developers could alternately squash merge this branch to prevent the large file addition to the history.

signac_dashboard/templates/cards/plot_viewer.html Outdated Show resolved Hide resolved
signac_dashboard/templates/plot_viewer/js/plot_viewer.js Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Nov 27, 2022

Codecov Report

Merging #163 (24a9aa2) into master (21c3cb7) will decrease coverage by 0.19%.
The diff coverage is 70.58%.

❗ Current head 24a9aa2 differs from pull request most recent head 121578f. Consider uploading reports for the commit 121578f to get more accurate results

@@            Coverage Diff             @@
##           master     #163      +/-   ##
==========================================
- Coverage   73.83%   73.64%   -0.20%     
==========================================
  Files          20       21       +1     
  Lines         795      846      +51     
==========================================
+ Hits          587      623      +36     
- Misses        208      223      +15     
Impacted Files Coverage Δ
signac_dashboard/modules/plotly_viewer.py 70.00% <70.00%> (ø)
signac_dashboard/modules/__init__.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@bdice
Copy link
Member

bdice commented Nov 27, 2022

Do you consider that a new test is needed for the module?

Explicit tests aren't needed -- the testing framework is pretty weak right now, and this should be automatically covered by the "kitchen sink" tests. However, an example would be good to include.

@Charlottez112
Copy link
Member

I agree with Bradley. This seems very useful. Thank you for the implementation! If you could include an example, I'll also test it locally on my machine.

Copy link
Author

@javierbg javierbg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes for #163

javierbg added a commit to javierbg/signac-dashboard that referenced this pull request Nov 28, 2022
@javierbg
Copy link
Author

I'm not familiarized with Flask, so when it came to add the routes and assets I just used other modules as reference (I think it was the DocumentEditor module).

When adding the Plotly CDN URL I realized that the "filename" attribute in line 104 was not used, apparently, so I removed it. Is this ok?

@javierbg javierbg force-pushed the feature/issue-162-plotly-plots branch from c55b48f to a3afa94 Compare November 28, 2022 11:58
@javierbg javierbg requested a review from a team as a code owner November 28, 2022 11:58
javierbg added a commit to javierbg/signac-dashboard that referenced this pull request Nov 28, 2022
@javierbg javierbg force-pushed the feature/issue-162-plotly-plots branch from a3afa94 to c55b48f Compare November 28, 2022 11:59
Copy link
Member

@joaander joaander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the addition. However, you need to protect access to the new routes with flask_login.

I'm not able to add a code suggestion for this as your module does not add routes with the same pattern as the others. See the changes in #158 for examples.



def plot_viewer_asset(filename):
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protect this route from access by unauthenticated users.

@javierbg javierbg requested review from joaander and removed request for tcmoore3 and Charlottez112 November 28, 2022 16:37
Copy link
Member

@joaander joaander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That should work to protect the new route.

bdice added a commit to javierbg/signac-dashboard that referenced this pull request Nov 28, 2022
@bdice bdice force-pushed the feature/issue-162-plotly-plots branch from 839d41b to 6ebebf9 Compare November 28, 2022 18:25
@bdice bdice force-pushed the feature/issue-162-plotly-plots branch from 6ebebf9 to a898191 Compare November 28, 2022 18:52
examples/plots-plotly/dashboard.py Outdated Show resolved Hide resolved
signac_dashboard/modules/plot_viewer.py Outdated Show resolved Hide resolved
signac_dashboard/modules/plot_viewer.py Outdated Show resolved Hide resolved
@@ -0,0 +1,12 @@
function draw_plot(element) {
data = JSON.parse(element.getAttribute("data-plotly-data"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the card HTML can contain a REST endpoint, and fetch the data with a second call? That way we don't have to generate and include all the raw data in the HTML page. This should help a lot with page responsiveness. You can see examples of REST calls in other modules:

If you need help with this, let me know.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have implemented this in 226111e. The page loading is way more responsive now.

Still, I'm a total newbie with Flask, so a thorough review is needed.

My main concerns are:

  • I have had to create a module identifier so that a different endpoint is created for each instance of the PlotlyView module. I decided to hash the id(self) which should work, but has the disadvantage that each time the dashboard is launched the URLs change. I don't think this is a huge problem, but I feel like there is a better way to do this.
  • For each added instance of the module, the register method is called. However, the assets URLs should only be added once. If you try to do it multiple times, the second time around the module is not registered. I have solved this with a class attribute PlotlyViewer._assets_url_registered that avoids this duplication. Still, I feel like this solution is somewhat hacky.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javierbg
Flask Blueprints are the way to go. I started experimenting with them in #182 to allow multiple Notes modules to be added.

Javier Barbero and others added 5 commits November 28, 2022 20:26
Now instead of passing the data as HTML attributes a REST endpoint
has been set up so that the page is rendered quickly and the client
can fetch the data with a GET request
Javier Barbero and others added 4 commits November 29, 2022 15:47
The Project.id and Job.id properties were not available in
signac 1.0.0. The fix changes it to use the get_id() method
in both cases.
@cbkerr cbkerr self-assigned this Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants