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

Pelican-plotly with Ipython #1

Open
datavistics opened this issue Sep 4, 2017 · 9 comments
Open

Pelican-plotly with Ipython #1

datavistics opened this issue Sep 4, 2017 · 9 comments

Comments

@datavistics
Copy link

How can I use this with the pelican ipython notebook plugin?
https://github.com/danielfrg/pelican-ipynb

Thanks!

@ran404
Copy link
Owner

ran404 commented Sep 5, 2017 via email

@datavistics
Copy link
Author

Thanks, will do! Btw, your work is amazing. #goalz

@datavistics
Copy link
Author

@ran404 I know it isnt exactly your issue but Ive been looking pretty hard and cant find and answer.

Do you have any advice on getting plotly to work with jupyter and pelican?

Here is what I have tried:
Reddit /r/learnpython post
Messaging the pelican-ipynb author

Adding plotly-latest.min.js to the tags of the html output

I have a functional notebook, and verified that if I run the notebook I can get plotly out when I export to html.

Thanks so much,
Derek

@ran404
Copy link
Owner

ran404 commented Sep 9, 2017

hey @datavistics, it's interesting that you asked, because many months ago I've come across this exact problem, and i did actually come up with a solution! Far from perfect, it does seem to work: you can see an example on one of my blog pages:
http://www.arraystream.com/blog/fft-based-option-pricing.html

I really hope I could devote some time writing down my solution, but the basic idea was that since it was too difficult to render the jupyter notebook with all the correct css and javascript on the same html page, i've cheated by simply embedding the html inside an iframe. The rough steps were:

  1. jupyter nbconvert <your-notebook>.ipynb. This create a standalone html with all the css and javascript in-place. Save this file in content/notebooks folder.

  2. Add the following lines to your theme's templates/article.html, the exact file depend on the theme you use but most should follow a similar filename convention. I've added the extra code just after the {{ article.content }} macro. The code checks for existence of the "notebook" variable definition in the blog post. If it exists, it creates an iframe and embeds the notebook html, along with some javascript code that handles re-rendering when the browser window is resized.

            <!-- #####End post metadata-->
            <!-- #####Begin post contents-->
            <div class="post-body">
                {{ article.content }}
                {% if article.notebook %}
                <iframe id="jupyter-notebook-iframe" class="render-viewer" src="{{ SITEURL }}/{{ NOTEBOOKS_DIR }}/{{ article.notebook }}"></iframe>
                <script type="text/javascript">
                    var jupyterIframe = document.getElementById('jupyter-notebook-iframe');

                    var resizeId;
                    $(window).resize(function () {
                        clearTimeout(resizeId);
                        resizeId = setTimeout(function () {
                            doneResizing();
                        }, 500);
                    });

                    function doneResizing() {
                        jupyterIframe.style.height = jupyterIframe.contentDocument.body.scrollHeight + 'px';
                    }

                    window.onload = doneResizing;
                </script>
                {% endif %}
            </div>
            <!-- #####End post contents-->
  1. You need to specify some extra config in pelican.conf, otherwise pelican will get very confused about the existence of html files inside the notebooks folder.
# Ignore html files in notebooks directory, or they'll get picked up by pelican
PAGE_EXCLUDES = ['notebooks']
ARTICLE_EXCLUDES = ['notebooks']

NOTEBOOKS_DIR = 'notebooks'
STATIC_PATHS = ['images', 'plots', NOTEBOOKS_DIR, 'extra/robots.txt', 'extra/favicon.ico']
  1. Finally, you create a blog post markdown file like the following. The Notebook variable is a new concept we've added.
Title: FFTOptionLib
Slug: fft-based-option-pricing
Summary: FFT-based Option Pricing Methods in Python
Category: blog
Authors: Yu Zheng, Ran Fan
Date: 2017-05-08
Notebook: fftoptionlib-blog.html

I hope this makes some sense, it's been too long and I may have forgotten some details. All I can say is that there is definitely improvement that can be made in the jupyter side to make this sort of thing easier! I've wasted a lot of time on this very issue and to be honest I wish all of this could be achieved with a wordpress plugin, rather than messing around with static site generators and writing custom code!

@datavistics
Copy link
Author

datavistics commented Sep 9, 2017

Thank you x1000. Seriously, you have helped me so much.

If anyone tries this:
I found that you have to view the code in localhost or the production url to work with the iFrame. Also replace the underscores with hyphens in your file names.

@ran404 I did have one last issue. The code to do the resizing isnt working here is what it looks like:
image

Im using this the flex theme.

I added the code you had underneath the {{ article.content }} and changed the div to "post-body"
You can see exactly what the code I added in this gist.

@datavistics
Copy link
Author

datavistics commented Sep 10, 2017

I think it has to do with one of 2 things more than likely the first:

  1. I get an error message: WARNING:root:Unable to find '/Notebooks/custom.css' or variations.
  2. The div in the theme was originally just <div> and I changed it to <div class="post-body"> like in your example.

Any ideas @ran404 ?

Thanks again so much!

Update: I noticed your notebook/ipynb.html file had this

<style type="text/css">
--
  | /*
  | Placeholder for custom user CSS
  |  
  | mainly to be overridden in profile/static/custom/custom.css
  |  
  | This will always be an empty file in IPython
  | */
  | .container { width:95% !important; }
  |  
  | </style>

So I added it to mine and the custom.css error went away. I could change my theme to yours and give that a shot? The real issue seems different than the theme though. Its that the iframe is not resizing.

Im out of ideas :(

@datavistics
Copy link
Author

datavistics commented Sep 11, 2017

Update 2:

I found out that the jquery module was missing so I imported it! We are almost there, now my issue is that the width is off, do you have any ideas why that is?

image

@datavistics
Copy link
Author

Last update:
I got it working here... Thanks again for all your help!
danielfrg/pelican-jupyter#75 (comment)

@ran404
Copy link
Owner

ran404 commented Sep 12, 2017 via email

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