ipywidget interactive plot in Html presentations ipython notebook - jupyter-notebook

I would like to use the interactive function from ipywidget in an ipython notebook to make a presentation in notebook.
I have stored my data in a pickle file and what I want is to change the parameters interactively so that I can see my plot.
My code is like this.
def spin(model, power):
with open(path_cluster1+'SSHFS/Model'+str(model)+'/'+str(power)+'/spinpython2.pickle','rb') as f:
spin = pickle.load(f)
plt.plot(spin)
plt.title('Power'+str(power*0.1))
interactive(spin, model=(1,4,1), power=(70,101,1))
My collaborators are unfamiliar with python so in principle I would like to make the life easier for them to see my data just by changing a parameter in a html page. Maybe I have to save all the data in a pickle file but the question is if this can work in a html without running python.
Is something like this possible?

Have a look at https://www.nbinteract.com/. To quote from the website "nbinteract is a Python package that provides a command-line tool to generate interactive web pages from Jupyter notebooks."

Related

Render docstring with Sphinx in Jupyter

Is there a way to make Jupyter notebooks render docstrings that contain Sphinx markup correctly in a Jupyter notebooks.
At the moment if I have a Foo class with Sphinx documentation and the user tries to get the docstring with
[1] Foo.bar?
They end up with the raw text. E.g.
Calls the :py:meth:`bar` method of the :py:class:`Foo` class.
Is there a way to make Jupyter automatically render the docstring correctly as Sphinx. Or anything that's easier to read than raw rst?
SageMath does this, I believe using its "sphinxify" code: https://github.com/sagemath/sage/blob/develop/src/sage/misc/sphinxify.py. That is not easy to use out of the box — it has various SageMath specific things in it — but maybe you can adapt it.

Interactive R Notebooks/Dashboards for Non-R Users

Not sure this is the right place for this kind of question, but I am looking for ideas on how to share some R Markdown Notebooks with non-R users, but that still have some dynamic functionality in them. For example, I can create a notebook with html-widgets to adjust time windows, choose different data points, etc. I compile this in R Studio and it looks great on my machine. But sharing the .nb.html file with co-workers, they don't get the same level of interactivity, which is expected.
Is there a way to share these files or similar R files with non-R users? Additionally, I don't have access to a personal/company website where I could host these notebooks.
you could for example use :
DT package to give your Rmd output filter- able html data frames .
you can also use plotly() to have interactiv plots [easy to do for ggplot2 users using ggplotly() ]
further check out :
flexdashboards https://rmarkdown.rstudio.com/flexdashboard/
sematic dashboards. https://cran.r-project.org/web/packages/semantic.dashboard/semantic.dashboard.pdf
both are solutions for more dynamic Rmarkdown reports, but not as demanding to programm as full blown shiny apps.

Using plot within a script in julia

I've been prototyping some julia code in a Jupyter notebook, but it's outgrown the notebook format and I want to put it in a script in order to organise it properly. However, I can't work out how to get Plots to work in a script, and can't find any documentation about it.
Here's a minimal not-working example:
using Plots
gr()
display(plot([1,3,2]))
println("here")
This code takes 20-30 seconds to import Plots, then opens a window but immediately closes it again, prints "here", and exits. I can't use ctrl-C while the import process is happening.
So, I have three questions:
How do I prevent the plot window from closing as soon as it opens? What I want is for the script to block or (ideally) enter an event loop until the window is closed, and terminate after that.
Can the extremely long import time be reduced somehow?
Does any documentation exist for using Plots outside of a Jupyter environment?
If it makes a difference, I'm using julia 1.1.1 on a Mac.
The most natural way to achieve the workflow you're looking for in the first bullet is to use Juno or VS Code. Suppose in your working directory you have the following code in a file script.jl:
using Plots
gr()
display(plot([1,3,2]))
hello = "world"
println("here")
Now fire up Juno or VS Code and launch the integrated Julia REPL. From the Julia REPL, you can use include to source your script:
julia> include("script.jl")
Plot{Plots.GRBackend() n=1}
here
Now your script has been completely run and the plot is available for viewing in the plot pane (this is true for both Juno and VS Code). Now you can continue to include other files in the REPL or run other code interactively. Note that variables defined in script.jl are available for use:
julia> hello
"world"
EDIT:
If you run a script using the -i option (for interactive), the plot GUI will stay open and a REPL will launch after the script is done running:
$ julia -i script.jl
The various options to the Julia executable can be found here, or can be found by typing $ julia --help in a terminal.
The import time can be eliminated by compiling the Plots.jl package ahead of time and baking it into your julia executable using the PackageCompiler.jl package. Here is a link to the package website
https://julialang.github.io/PackageCompiler.jl/dev/
See the tutorial on how to do this, the first tutorial is about compiling a syntax highlighting package. That one replaces your default 'sysimage'. The second tutorial is about creating a loadable 'sysimage' where Plots.jl is compiled, and starting Julia with this 'sysimage' so it doesn't have to complie Plots.jl again.
However I coudln't get the second 'loadable sysimage' working so I used the first tutorials way even for Plots.jl.
After this change, using Plots and plot() are basically instant.

Jupyter Notebook save to PDF without Code

This has been an ongoing issue for me, as I would love to use Jupyter Notebook to write my research reports, but have found it very difficult to export my Jupyter Notebooks to PDF without code and without large formatting errors.
I am able to download the notebooks as PDF, but have not found a way to hide the code, or have the PDF resemble the formatting of the notebook.
No solution I've found on SO has been sufficient for my issue, so it may be possible that this is not the intended functionality of Jupyter Notebook.
Have you tried this:
jupyter nbconvert path/to/your/ipynb --to=pdf --TemplateExporter.exclude_input=True
For more on the flags, you can refer to nbconvert config options
I have successfully converted .ipynb to .html exclude the code blocks
I followed the suggestion by 0xffff above but the PDF output did not behave as intended (code blocks were still included).
However, their suggestion inspired me to try converting to html first using the following call:
jupyter nbconvert path/to/your/ipynb --to=html --TemplateExporter.exclude_input=True
This behaved as intended, and from there, it was straightforward to print the output to PDF in a browser.
Regarding the issue with the --to=pdf flag, I've opened up an issue on the Jupyter Notebook git repo: https://github.com/jupyter/notebook/issues/3804. Will report back once I get a response.
nbconvert
nbconvert is an official package, and other answers have mentioned it. However, they did not mention there are two ways to convert to PDF.
The default option uses LaTeX, and it's often a pain to install:
jupyter nbconvert notebook.ipynb --to=pdf
A newer option is to use the webpdf converter, this does not require LaTeX
jupyter nbconvert notebook.ipynb --to=webpdf
Quarto
Quarto is the newest option. The defaults are prettier than nbconvert, and it has advanced features to customize the output PDF. However, it uses pandoc (and pandoc uses LaTeX), so you must install both first.
quarto render notebook.ipynb --to pdf
Quarto is a new project, and I often run into problems (I couldn't convert the sample notebook); however, the project is in active development and I'm sure it will get better.
Online converters
The last option is to use one of the many online services. The two main caveats are 1) You might not want to upload your notebooks to some random website, and 2) It's often unclear what engine they use to convert the notebooks. I've tried many of them with mostly negative results.
I created one online converter that uses nbconvert and deletes your notebook as soon as it's converted.
If you manually create the PDF, there is a simpler solution: add the following code block & execute it
from IPython.core.display import HTML
HTML('<style>.input, .jp-InputArea {display: none !important}</style>')
Code will disappear both in the "normal" UI and in print preview / export to HTML. Open print preview, generate the PDF - it looks great now, clear the output of this one cell to bring the code back, done

How to use inline code in markdown in Jupyter with R

How to use inline code in markdown in Jupyter with R?
CODE CELL
n <- 8
MARKDOWN CELL
The Number is {{n}}
What would be the correct syntax in Jupyter Markdown for R? Is it even possible?
In case you haven't done it already, you might want to give the python markdown extension a shot, that adds the markdown inline code functionality for Python to Jupyter notebooks. On their github they claim:
The Python Markdown extension allows displaying output produced by the current kernel in markdown cells. The extension is basically agnostic to the kernel language, however most testing has been done using Python.
Installation instructions are on the github page of the nbextensions. Make sure you'll enable the python markdown extension using a jupyter command or the extension configurator.
Calling variables then should work inside a markdown cell with the {{var-name}} syntax that you've already given (described in the readme of the corresponding github page (linked in the wiki)).
If this doesn't help, you might want to join the discussion of the corresponding issues in the issue trackers for ipython and jupyter.

Resources