Posting IPython Notebook in Wordpress - wordpress

I am trying to convert my IPython notebook to an html file so that I can put it on my wordpress blog. I have used the following command to generate an html file for the notebook
ipython nbconvert notebook.ipynb
then I copied the html code and pasted it into the 'text' tab. The resulting blog post sort of looks like the ipython notebook, however the problem is that the markdown equations do not show up and the headings look strange. Has anyone managed to display an IPython notebook in a wordpress blog post successfully? If so, how?

In this November 2013 blog article http://www.josephhardinee.com/blog/?p=46, the author goes quickly through the conversion process.
He mentions the need to install the Simple Mathjax plugin to make equation display work.
Now, what I have tested to work on my self-hosted Wordpress blog:
Copy paste the html output of nbconvert (only what is inside the <body> tag) in the "Text" tab.
disable the Worpress html code parsing because otherwise images do not display (as explained in the blog post). See below for two possible methods.
Activate Mathjax: either with a plugin or manually in the post code
Mathjax With plugin
I have not tested the Simple Mathjax plugin, but I have LaTeX for WordPress which works for me.
Manual Mathjax activation
Copy paste from nbconvert output the two <script> tags that activate Mathjax:
1) Load the library:
<script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"></script>
2) Launch it:
<script type="text/javascript">
init_mathjax = function() {
if (window.MathJax) {
// MathJax loaded
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
},
displayAlign: 'left', // Change this to 'center' to center equations.
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}}
}
});
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}
}
init_mathjax();
</script>
Disabling code HTML parsing
The blog post suggests to activating the PS Disable Auto Formatting plugin to make the notebook images work. I have tested it successfully but it has one drawback: it messes up with the rendering of all the other posts... that's quite an issue!
I have tested instead the Raw HTML plugin which enable a per-post tuning. I've made images work by selecting the Disable automatic paragraphs option (the plugin creates a new box in the post editor).
Remaining issues:
while the notebooks should display fine with this method, there is still work to get the syntax highlighting of code cells to display properly. However the Python source code is already parsed by CodeMirror, so it should just be about loading the appropriate CSS code.

One approach is to embed the notebook using an iframe. This original idea came from the blog post http://onsnetwork.org/eimd/2014/08/08/how-to-enter-ipython-notebooks-to-wordpress/, but I've made several improvements. The straightforward way to do this is to:
Install the Raw HTML plugin to Wordpress. This only needs to be done once.
Convert the notebook to HTML ipython nbconvert YOUR_NOTEBOOK.ipynb
Upload the resulting HTML to Wordpress as a media file. Take note of the URL where it is uploaded to.
Insert the iframe between raw tags in your post. For example:
[raw]
<iframe id="ipython_notebook_frame"
style="height: 500px; width: 100%; padding: 0; border: none;"
src="URL_OF_NOTEBOOK">
</iframe>
[/raw]
Unfortunately, this straightforward approach doesn't work very well since the height is never right and one tends to get annoying horizontal scroll bars. However, since the uploaded html is hosted on the same domain as the blog post, this can be fixed using some javascript. The following recipe seems to work reasonably well for getting the width and height right, resulting in a clean blog post:
[raw]
<script type="text/javascript">
function resizeIframe(ifrm) {
ifrm.style.height = ifrm.contentWindow.document.body.scrollHeight + 'px';
// Setting the width here, or setting overflowX to "hidden"both
// seem to work. It may be that one turns out to be better; for
// now I set the height.
ifrm.style.width = ifrm.contentWindow.document.body.scrollWidth + 'px';
}
</script>
<script type="text/javascript">
function onLoad() {
var ifrm = document.getElementById('ipython_notebook_frame');
setTimeout(resizeIframe, 0, ifrm);
}
</script>
<iframe id="ipython_notebook_frame"
style="height: 500px; width: 100%; padding: 0; border: none;"
src="URL_OF_NOTEBOOK">
</iframe>
<script type="text/javascript">
// By deleting and reinstating the iframe src, and by using setTimeout
// rather than resizing directly we convince Safari to render the
// page. I've lost the link for where I found this trick, so
// can't properly credit it.
var iframe = document.getElementById('ipython_notebook_frame');
iframe.onload = onLoad;
var iSrc = iframe.src;
iframe.src = '';
iframe.src = iSrc;
</script>
[/raw]
For a little more detail on this, as well as an example you can take a look at this post: http://www.bitsofbits.com/2015/01/19/ipython-notebooks-in-wordpress

Related

Change images initial width in a "before and after" wordpress plugin

On my website (which is under construction) I'm using "Before-After MultiX Slider". It's working fine, but I'm trying to have all the separators "collapsed" on the right (or left).
For example here
I've tried to use css to change width of some classes as follows:
.wmg-image.wmg-image-3.first.ui-resizable {
width: 91.6379%!important;
}
.wmg-image.wmg-image-2.ui-resizable {
width: 95%!important;
}
.wmg-image.wmg-image-1.ui-resizable {
width: 98.3621%!important;
}
If I don't use !important nothing happens. If I use it, I get what I want
but the slider stops working and the images don't resize by scrolling separators.
Any idea on how to achieve this?
I can't find any failure there and digged a little deeper into the problem... Therefore I post a second answer, because the nature of the problem is somehow different after all, the code is working but not as expected for the following reason:
If you load scripts async the page will continue to render while the scripts are still loading, so it is possible that other none async scripts comming afterwards will start to load earlier than some async ones still in query...
Here we don't have async ones, BUT it seems to be like even if the async keyword is not present though the scripts will come in the query one after an other depending on their order nevertheless the server will load round about 3 or 4 scripts parallel!
-> So it happens that shorter scripts may be finished to load before longer ones allthough they've started to load later...
On my research I could not find a definite solution for that problem, because it simply seems to be quite hard to control that loading process in detail! (You i.e. can find some topics about that phenomena here on the board...)
I will present some different approaches, you will have to test them yourself, because I don't have that slider PlugIn so I could try:
Solution 1:
Try to use the "defer" keyword, this should be so to say the opposite of "async" and cause scripts to be not loaded before the page is parsed completly, but sadly I'm not sure if that works, never used it before and it sounds like it is the same as using "document.ready" which is not working in this case...
Important thing is that you must insert the script externally otherwise the keyword won't do anything, i.e:
<script src="demo_defer.js" defer></script>
Solution 2:
A surely working solution would be to add our script as a callback to the call of the slider script, but I guess that this is no suitable solution here because you'll have to change the PlugIn code which is not update safe!
Solution 3:
Maybe you can play with a timeout to make sure that the execution will start later, but the problem is that you cannot really know how long this timeout must be! (i.e. have a look here: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)
WORKAROUND:
I thought up a way to get around that problem, but as said before the code is untested, maybe try that and give me a notice when you have included this script, so I can have a look how it works...
In this way it is supposed that a class which does the positioning with the !important-statement is added and then removed on the first click, just then the elements are positioned again without !important and of course after that on every further click the positioning won't be manipulated again!
function sr_custom_width_for_slides()
{ ?>
<style type="text/css">
.notClicked .wmg-image.wmg-image-3.first.ui-resizable {
width: 91.6379% !important;
}
.notClicked .wmg-image.wmg-image-2 {
width: 95% !important;
}
.notClicked .wmg-image.wmg-image-3 {
width: 98.3621% !important;
}
</style>
<script type="text/javascript">
jQuery( document ).ready(function() {
var firstClicked = false;
jQuery(".wmg-before-after").addClass("notClicked");
jQuery(".wmg-before-after").click(function() {
if ( !firstClicked ) {
jQuery(".wmg-image.wmg-image-3.first.ui-resizable").css("width","91.6379%");
jQuery(".wmg-image.wmg-image-2.ui-resizable").css("width","95%");
jQuery(".wmg-image.wmg-image-1.ui-resizable").css("width","98.3621%");
jQuery(".wmg-before-after").removeClass("notClicked");
firstClicked = true;
}
});
});
</script>
<?php }
add_action( 'wp_footer', 'sr_custom_width_for_slides', 1000 );
EDIT REGARDING YOUR LAST COMMENT:
I checked the site again and in my case it sadly does not work at all, because the click event is never fired! I guess that the JS code of the PlugIn binds some events which maybe stop our script from working... (See the function "stopPropagation()" for more infos.) So my final clue is to simply bind another event which hopefully is not manipulated by the PlugIN in that way! As far as I can see this could be "mouseenter" or "mouseover"...
So just change
jQuery(".wmg-before-after").click(function() {
...
});
to
jQuery(".wmg-before-after").mouseover(function() {
...
});
the problem is quite simple... The width is set inline via js, so this will override any changes you made in your css file!
If you set the styles with an !important-statement it will work, but the sliders script cannot set the new width anymore...
So after all the most easy way to achieve what you want is to insert a small script which sets the styles AFTER the slider scripts have been loaded, so maybe at the very bottom of the footer of the page after the "wp_footer" call, because most plug ins enter their js over this hook, somehow like this:
<script>
jQuery(function() {
jQuery(".wmg-image.wmg-image-3.first.ui-resizable").css("width","91.6379%");
jQuery(".wmg-image.wmg-image-2.ui-resizable").css("width","95%");
jQuery(".wmg-image.wmg-image-1.ui-resizable").css("width","98.3621%");
});
</script>
I can't test it, but I'm quite sure that this will work, if the script is inserted at the correct position! :)
EDIT: I made a quick test via the console of the FF inspector and it works as expected, but as mentioned above, if the slider script will load later than this script it won't work at all!

How do i avoid a flash of unstyled content (FOUC) on Google Sites

I have placed some custom HTML, CSS, and jQuery inside an HTML box in my google site. but as the page loads, the unformatted content shows for several seconds until the loading is complete. attempts to add the following:
html { visibility: hidden; }
and then turning it back on later in jQuery do not appear to work inside a Google site.
Does anyone have another suggestion?
I don't know if this works on a google site or not, but using
<body style='display:none;'>
and then in the jQuery document ready function place
$("body").show(); has worked for me.
For some reason using style on the body statement works faster than the CSS file even if the CSS file appears in the head section.

jsPDF addHTML() only working for document.body

I am attempting to create a pdf of a page from my meteor powered site. The page contains quite a bit of styling and some d3 charts.
I have added chipcastledotcom:jspdf and new3rs:html2canvas to accomplish my goal. My plan is to use jsPDF's .addHTML() function to add the non chart html to the pdf. My issue is that unless I pass in document.body as the source for .addHTML() the rendered element is blacked out in the pdf. Using document.body I am able to see the currently viewable material on the webpage but nothing else (the page has quite a bit of content and requires scrolling). Is there some lack of support in jsPDF for certain levels of complexity w.r.t. to the webpage layout/styling?
More importantly, is there a fix/work-around for this?
Here is a jsfiddle (works perfectly) that demonstrates what I'm trying to accomplish: https://jsfiddle.net/jdcast/tqfdkusp/.
To be clear, the .addHTML() in the fiddle works well:
doc.addHTML(document.getElementById('div1'), 0, 0, function() {
console.log('callback');
doc.save('sampler-file.pdf');
});
However, in my site's code which is verbatim what is in the fiddle, I have to use the following to see any output that ISN'T blacked out:
doc.addHTML(document.body, 0, 0, function() {
console.log('callback');
doc.save('sampler-file.pdf');
});
Note also that I have used jsPDF's .text() function and it seems to behave properly so I don't think I have installed it incorrectly.
Thanks!

Is there a way to embed github code into an iframe?

The question may seem confusing so let me clarify.
Github lets you see the source code of a files in a repo. I want to include them in iframes but am unsure how and suspect someone has already done this before.
In my case I want to add https://github.com/ileathan/hubot-mubot/blob/master/src/mubot.coffee to an iframe so that from my website people can see the code as it evolves.
A GitHub page itself wouldn't be put directly in a iframe (because of the X-Frame-Options: deny HTTP header).
That leaves you with the GitHub API for contents
GET /repos/:owner/:repo/contents/:path
Like: https://api.github.com/repos/ileathan/hubot-mubot/contents/src/mubot.coffee.
You should be able to put that content in a iframe (as in this answer)
Here's a concrete example of how this can be done via the GitHub API. We request the encoded content and insert it directly into the iframe.
<iframe id="github-iframe" src=""></iframe>
<script>
fetch('https://api.github.com/repos/ileathan/hubot-mubot/contents/src/mubot.coffee')
.then(function(response) {
return response.json();
}).then(function(data) {
var iframe = document.getElementById('github-iframe');
iframe.src = 'data:text/html;base64,' + encodeURIComponent(data['content']);
});
</script>
Here's the code in action https://jsfiddle.net/j8brpdsg/2/
I just found a way to do this using Gist-it
Usage
Take a github file url and prefix it with http://gist-it.appspot.com and embed the result within a tag:
<script src="http://gist-it.appspot.com/http://github.com/$file"></script>
Here's a test I just made. Works! :)
You'll need to hack the iframe and css a bit to get it to work without tags in your document, but it's possible:
https://www.arctype.co/blog/embedding-github-gists-via-iframe
<iframe frameborder=0 style="min-width: 200px; width: 60%; height: 460px;" scrolling="no" seamless="seamless" srcdoc='<html><body><style type="text/css">.gist .gist-data { height: 400px; }</style><script src="https://gist.github.com/sundbry/55bb902b66a39c0ff83629d9a8015ca4.js"></script></body></html>'></iframe>

Trying to use the swfobject.js youtube object inside of WordPress

I need to use the extra powers of the swfobject api. This object is a new way of embedding Youtube videos into web sites.
Pasting code that I found from Google's tutorial directly into the WordPress editor was in-effective. WordPress would not treat this as active code.
So, I created a new template file and inserted my code into that file. This worked relatively well. The code went live and I got the extra feature that I was looking for, which was that I am able to have the visuals of the video autoplay, and to have the sound muted by default.
However, this has messed up the layout and flow of my menus which where just above the video.
Can anyone tell me where to proper place to put this code is, or is this question too specific. If it will help you can see the messed up page at:
http://bestoftimesusa.com/home-mute-test/
and how it is supposed to look at:
http://bestoftimesusa.com
The fully functional code that got embedded is this:
<script type="text/javascript" src="/wp-includes/js/swfobject/swfobject.js"></script>
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<script type="text/javascript">
var ytplayer = false;
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/IBjstQceGBk?enablejsapi=1&playerapiid=ytplayer&version=3&autoplay=1",
"ytapiplayer", "370", "238", "8", null, null, params, atts);
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.mute();
}
</script>
</div>
Unless you want the same youtube video to appear on all pages (of a certain type), I don't think putting that entire block in your template files makes sense. The only part that really makes sense for a template file is the first line. The lines after that are video-specific.
By default, WordPress filters out javascript from posts. You can disable that filtering with a plugin which would allow you to include javascript in your posts.
Using that plugin, you can set javascript filters on a global or per-post basis. It seems like a per-post basis would work for you so I'd go with that, just enabling it on the page I wanted.
Two last things:
You could put the first line in one of your template files to eliminate having to put that in every post
You have one opening <div> tag but two closing </div> tags, that could be expected, but I'd double check.

Resources