Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Rendering HTML in examples #199

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

Closed
rth opened this issue Jan 26, 2017 · 2 comments
Closed

Rendering HTML in examples #199

rth opened this issue Jan 26, 2017 · 2 comments

Comments

@rth
Copy link

rth commented Jan 26, 2017

Since sphinx-gallery displays the example output on a web page, I was wondering if it was possible to render directly some HTML produced by the example script.

For instance, in jupyter notebooks, one can render some HTML with,

from IPython.display import display, HTML
display(HTML('<span style="background-color:red;"> test</span>'))

so if the example .py script contained a display(HTML(...)) call, is it possible to just embed the corresponding HTML code into the example page? I could not find anything about this topic in the docs.

If that not currently supported, what would be the advised approach to add this functionality (either through display(HTML(...)) or some other mechanism)? Thank you.

@Titan-C
Copy link
Member

Titan-C commented Jan 26, 2017 via email

@rth rth changed the title Support of IPython.display.HTML Rendering HTML in examples Mar 14, 2017
@rth
Copy link
Author

rth commented Mar 14, 2017

@Titan-C Thanks a lot for this exhaustive response and sorry for my very late reply..

Following your explanations, I managed to make it work. Assuming that,

  • examples are under examples/
  • the docs are under doc/
  • that the docs are built with make html from under docs,

in order to render HTML contained in the html_data variable, adding the following lines at the end of the example script, seems to work,

tmp_dir = os.path.join('..', 'doc', 'examples')
if os.path.exists(tmp_dir):
    # building the docs with sphinx-gallery 
    with open(os.path.join(tmp_dir, 'out.html'), 'wt') as fh:
        fh.write(html_data)
else:
    # running from a terminal or jupyter
    from IPython.display import display, HTML
    display(HTML(html_data))
 
####################################
# .. raw:: html
#     :file: out.html

The path manipulations are not very clean and I'm not sure how to control the order in which HTML, figures, and stdout are rendered, but in any case this works for my use case . A more complete example can be found here. Closing this issue, thanks!

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

No branches or pull requests

2 participants