I'm interested in placing a javascript powered graph into my jupyter notebook file as a div. This website uses a code magic approach, and pasting the graph code directly in a jupyter notebook cell:
http://blog.thedataincubator.com/2015/08/embedding-d3-in-an-ipython-notebook/
However, as many of such javascript powered graphs are lengthy, I think it would be desirable to simply call the javascript file that contains the graph's code, and subsequently appending it as a div in jupyter notebook. It sounds simple enough, but with all the path crisscrossing I have confused myself and I'm not sure what approaches I have tried are working or not.
I tried:
%%javascript
require.config({
paths: {
graph: 'filepath.js'
}
});
This did not throw any errors, but when I could't append any divs. I'm thinking perhaps the issue is actually the way data is linked between python and the js file. The above link uses an approach that points the data to window.vizObj={} I wonder if there are no simpler solutions?
Also, There is a converter library: http://mpld3.github.io/. Of course, I'm not against that, but for the sake of illustration, (and pride) I would kind of like to get to the bottom of this. Please feel free to share any/all thoughts on this matter.
If possible, please upload a jupyter notebook file so I can see how one goes about appending a js graph into jupyter notebook from a js file, and how the data is pointed. Any type of graph is ok, maybe a simple d3.js line graph.
Just do it from python
from IPython.core.display import Javascript
_filepath = 'filepath.js'
with open(_filepath, 'r') as _jscript:
code = _jscript.read()
Javascript(code)
Related
I searched a lot for this issue but didn't come to any straight to the point answer, so I am turning to you here and hopefully someone can help direct me to the right path at least.
The issue is simple, I have normal jupyter Nb and I would like to share it with others by sending them html format file. Using the normal !jupyter nbconvert --to html mynotebook.ipynbwill get the html export, but recently I started getting output with very wide screen (it uses the monitor screen).
How can I change the output screen size to maintain the same configurations even after exporting it to html?
In case my explanation wasn't clear I will add pictures:
notebook before exporting:
After exporting:
I don't want to create any special template, I just want to maintain the same parameters before exporting i.e. the width of the cells (inputs and outputs). Most of the answers I found here was talking about creating my own template or running some css code (both I don't have knowledge in...). Is there a ready to use template or argument that I can use to maintain the same layout?
I ran into the same issue. I think the problem might be that --to html uses the Jupyter Lab template by default. Once I added --template classic to my call to nbconvert the resulting HTML-file was much smaller and resembled the actual Jupyter notebook much more closely.
I am currently working on a custom Jupyter Widget. When I include the necessary javascript using %%javascript magic in the notebook I can simply reload these cells to update my widget if I make changes, which is handy for development.
Is there a way you can do this type of easy reloading with the javascript stored outside of the notebook? Because right now I have to completely restart Jupyter when I want to see the effects of a change.
A related question: when I store the javascript externally, Jupyter somehow still knows where to find it even though I don't load it explicitly. Where does Jupyter look to find the javascript for custom widgets?
Okay so apparently I just wasn't diligent enough in clearing my outputs, which is why I got weird results. I post my solution below in case it helps other people.
I have my javascript in separate .js files. I load this into my notebook by having a cell with the following content:
from IPython.display import display, HTML
with open("javascript.js") as f:
data = f.read()
text = """<div style="overflow:auto">Loading scripts</div>
<script type="text/javascript">
{}
</script>""".format(data)
display(HTML(text))
The execution of this cell inserts the necessary javascript. When the underlying javascript files have changed, just rerun the cell to update the javascript. Then when you instantiate a new widget, it will have the updated code.
I am quite new to Python and I have just discovered LIME for model prediction interpretation, I have followed code from this tutorial: https://www.kaggle.com/emanceau/interpreting-machine-learning-lime-explainer
I am wondering if there is a way of displaying the explanations as they are shown in Jupyter notebook without using notebook, i.e running python on a text editor. I have seen something about output_file but I can't figure out how to implement it with lime. I am hoping to see something like what is shown in notebook:
show_in_notebook img
Is this possible, or do I need to start using Jupyter Notebook?
You don't really need to start a jupyter notebook to display the explanations as they are shown with the help of show_in_notebook function.
LIME package provides save_to_file function that allows one to save explanations to .html pages. In a python script it would be something like this:
explainer=lime.lime_tabular.LimeTabularExplainer(X.values,feature_names=X.columns)
exp = explainer.explain_instance(data_for_prediction, model.predict_proba)
exp.save_to_file('lime.html')
There's also as_pyplot_figure that creates a barchart explaining the prediction. The output can be saved with matplotlib.savefig.
I have an interactive graph generated by Plotly in Python that I saved to an html file using plotly.offline.plot(fig, filename='/tmp/interactiveGraph.html')
I am now trying to embed this interactive graph into some kind of webpage, using either Dash or Django. I'm leaning toward Django at the moment, given that I have an html version of the graph. Which would be better?
My code for the webpage is in a separate file from the code where I am creating the graph.
A lot of the tutorials I've found online just say to add a few lines to the template, but I don't know how to get those lines that they've described.
tl;dr: I'm looking for guidance as how to integrate an html file-for a Plotly interactive graph-with a web python script using Django or Dash
Side Question:
what is the difference between
plotly.offline.plot(fig, include_plotlyjs=False, output_type='div')
and what I have above?
Reference:
https://github.com/ricleal/DjangoPlotLy
https://www.pythonsetup.com/how-implement-data-visualization-django-and-plotly/
I would highly reccomend Django, its a great framework. As for this, the best option is to generate the data via JavaScript and Plotly has a great library for this. If you must use python, then Django can be used. Assuming you are familiar with Django, inside of your view you can collect your data and build your graph ( I would reccomend a celery task for something long running like this unless they are small graphs), and you can then collect the return from creating the graph in div format. This will return a string that holds all the needed html and css for the graphs. Put this inside of you get_context_data() method in your view and place it into the dictionary. You can the use that object inside of a template. I have done this before, if you are having a hard time feel free to DM me. Hope this helps some!
In regards to your side question, I believe having False for including JS will make the graph a bit smaller assuming you have an include for the plotly JS library. They might have done this in a newer release to make the graphs faster as they were significantly slower in the browser from python that the JS rendered one.
I am using Jupyter notebooks with jupyter-contrib-nbextensions and I can fold sections as well as code blocks in them. I want to know if there is a way to preserve the folding feature when the notebook is exported to html. Currently when I export to html, I can't fold anything. The notebook becomes cumbersome to handle if it's long, even if there's a table of contents. I'll appreciate any ideas in this regard. Thanks!
Interesting need. I briefly looked into this. Looks like the nbextensions don't have that capability. I think there are perhaps two choices, both of which would require quite some work:
Take the html file and convert it to some form that would allow cascading style sheets and then manually implement folding
If you are looking to do this repeatedly and have the skills, then you could look at the nbextensions code and try to change it to make it fold
If you find any other options or solutions, post it here.
According to the docs for the "Collapsible Headings"-extension in nbextensions, you should be able to export a foldable html-file by using the html_ch format:
jupyter nbconvert --to html_ch FILE.ipynb