Title: [BUG]: frame paths relative to the html file when saving an animation to html · Issue #23581 · matplotlib/matplotlib · GitHub
Open Graph Title: [BUG]: frame paths relative to the html file when saving an animation to html · Issue #23581 · matplotlib/matplotlib
X Title: [BUG]: frame paths relative to the html file when saving an animation to html · Issue #23581 · matplotlib/matplotlib
Description: Problem Let's take the example from https://matplotlib.org/stable/gallery/animation/animate_decay.html import itertools import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def data_gen(): for cnt i...
Open Graph Description: Problem Let's take the example from https://matplotlib.org/stable/gallery/animation/animate_decay.html import itertools import numpy as np import matplotlib.pyplot as plt import matplotlib.animatio...
X Description: Problem Let's take the example from https://matplotlib.org/stable/gallery/animation/animate_decay.html import itertools import numpy as np import matplotlib.pyplot as plt import matplotlib.anim...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/23581
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG]: frame paths relative to the html file when saving an animation to html","articleBody":"### Problem\n\nLet's take the example from https://matplotlib.org/stable/gallery/animation/animate_decay.html\r\n\r\n```python\r\n\r\nimport itertools\r\n\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib.animation as animation\r\n\r\n\r\ndef data_gen():\r\n for cnt in itertools.count():\r\n t = cnt / 10\r\n yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)\r\n\r\n\r\ndef init():\r\n ax.set_ylim(-1.1, 1.1)\r\n ax.set_xlim(0, 10)\r\n del xdata[:]\r\n del ydata[:]\r\n line.set_data(xdata, ydata)\r\n return line,\r\n\r\nfig, ax = plt.subplots()\r\nline, = ax.plot([], [], lw=2)\r\nax.grid()\r\nxdata, ydata = [], []\r\n\r\n\r\ndef run(data):\r\n # update the data\r\n t, y = data\r\n xdata.append(t)\r\n ydata.append(y)\r\n xmin, xmax = ax.get_xlim()\r\n\r\n if t \u003e= xmax:\r\n ax.set_xlim(xmin, 2*xmax)\r\n ax.figure.canvas.draw()\r\n line.set_data(xdata, ydata)\r\n\r\n return line,\r\n\r\nani = animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init)\r\nplt.show()\r\n```\r\n\r\nIt works great and I can easily save the animation to html using code such as:\r\n\r\n```python\r\nfrom matplotlib.animation import HTMLWriter\r\n\r\nfname = \"path/to/myfile.html\"\r\nwriter = HTMLWriter(fps=5)\r\nwriter.frame_format = \"svg\" # Ensure svg format\r\nani.save(fname, writer=writer)\r\n```\r\n\r\nI can open the html animation from a browser or display it in a jupyter notebook:\r\n\r\n```python\r\nfrom IPython.display import HTML\r\n\r\n# Display the animation\r\nHTML(fname_html.read_text())\r\n```\r\n\r\nThe problem is that within the html file, the path to the frames is hardcoded rather than being relative to the \r\nhtml file. So if you change the location of the html file, it does not work anymore.\r\n\r\n```js\r\n\r\n\u003cscript language=\"javascript\"\u003e\r\n /* Instantiate the Animation class. */\r\n /* The IDs given should match those used in the template above. */\r\n (function() {\r\n var img_id = \"_anim_imga5cbaf4d0c4c4ffda374436a276eff4b\";\r\n var slider_id = \"_anim_slidera5cbaf4d0c4c4ffda374436a276eff4b\";\r\n var loop_select_id = \"_anim_loop_selecta5cbaf4d0c4c4ffda374436a276eff4b\";\r\n var frames = new Array(100);\r\n \r\n for (var i=0; i\u003c100; i++){\r\n frames[i] = \"path/to/myfile_frames/frame\" + (\"0000000\" + i).slice(-7) +\r\n \".svg\";\r\n }\r\n\r\n\r\n /* set a timeout to make sure all the above elements are created before\r\n the object is initialized. */\r\n setTimeout(function() {\r\n anima5cbaf4d0c4c4ffda374436a276eff4b = new Animation(frames, img_id, slider_id, 200,\r\n loop_select_id);\r\n }, 0);\r\n })()\r\n\u003c/script\u003e\r\n\r\n```\r\n\r\nYou can feed `ani.save(fname, writer=writer)` with a relative `fname` but in that case, it works in jupyter only if the html file is in the same folder than the notebook.\n\n### Proposed solution\n\nThe best would be to modify the javascript so it can get the path of the html file on the fly. I tried:\r\n\r\n```js\r\n\r\n\u003cscript language=\"javascript\"\u003e\r\n /* Instantiate the Animation class. */\r\n /* The IDs given should match those used in the template above. */\r\n (function() {\r\n var img_id = \"_anim_imga5cbaf4d0c4c4ffda374436a276eff4b\";\r\n var slider_id = \"_anim_slidera5cbaf4d0c4c4ffda374436a276eff4b\";\r\n var loop_select_id = \"_anim_loop_selecta5cbaf4d0c4c4ffda374436a276eff4b\";\r\n var frames = new Array(100);\r\n var scripts = document.getElementsByTagName(\"script\"), src = scripts[scripts.length-1].src;\r\n\r\n \r\n for (var i=0; i\u003c100; i++){\r\n frames[i] = src + \"myfile_frames/frame\" + (\"0000000\" + i).slice(-7) +\r\n \".svg\";\r\n }\r\n\r\n\r\n /* set a timeout to make sure all the above elements are created before\r\n the object is initialized. */\r\n setTimeout(function() {\r\n anima5cbaf4d0c4c4ffda374436a276eff4b = new Animation(frames, img_id, slider_id, 200,\r\n loop_select_id);\r\n }, 0);\r\n })()\r\n\u003c/script\u003e\r\n\r\n```\r\n\r\nAnd it works at least in Firefox and Chrome. However, it does not work for:\r\n\r\n```python\r\nfrom IPython.display import HTML\r\n\r\n# Display the animation\r\nHTML(fname_html.read_text())\r\n```","author":{"url":"https://github.com/antoinecollet5","@type":"Person","name":"antoinecollet5"},"datePublished":"2022-08-08T10:13:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/23581/matplotlib/issues/23581"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1b82e313-f12b-8d6c-c9e3-01e7a7f1e19b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9414:2E11B4:2D608D6:4077252:6A512356 |
| html-safe-nonce | 6e48569507dfe365212793df9596ab95f9fb3fc077dbd6405f785f81b2ad5151 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDE0OjJFMTFCNDoyRDYwOEQ2OjQwNzcyNTI6NkE1MTIzNTYiLCJ2aXNpdG9yX2lkIjoiNDk3NDUzNTU2OTAyNTYwNjQ4NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8500be47d4c619795a998084d1f9f3276e4fbef72220fb2bece4b3b1dd336d54 |
| hovercard-subject-tag | issue:1331643705 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/23581/issue_layout |
| twitter:image | https://opengraph.githubassets.com/95f66411a9c5caa573909a2ca319afb09ceed74b22edf72797e7d8c8b387af30/matplotlib/matplotlib/issues/23581 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/95f66411a9c5caa573909a2ca319afb09ceed74b22edf72797e7d8c8b387af30/matplotlib/matplotlib/issues/23581 |
| og:image:alt | Problem Let's take the example from https://matplotlib.org/stable/gallery/animation/animate_decay.html import itertools import numpy as np import matplotlib.pyplot as plt import matplotlib.animatio... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | antoinecollet5 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 689b683944b0d7f015d0e776452c970832fa81b82dc15284469b556ecced715b |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 942b51ed3bd5feee3d5e2719f890a5fe286edbbb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width