Render docstring with Sphinx in Jupyter - jupyter-notebook

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.

Related

Indenting VSCode like RStudio

I'm using VS Code and i miss RStudio's ctrl+i to indent lines.
I've read stuff online about LSP and other extensions and formatting with shift+alt+F but all i get is linter.
So, whats the equivalent if is there at all?
Found a solution but there a few things to take note.
First, VSCode notive options are language-agnostic, meaning it can't indent R code 'cause it doesn't understand it. You got to download extensions to make i work.
Extensions required are
Yuki Ueda's R for language support
REditorSupport's R LSP Client
Also, in order to R LSP Client to work, CRAN package {languageserver} is required.
Then, usage. If you're working functions, differently from RStudio in which one can hit ctrl+i and that'd indent that specifically line or selection, this solution works only if one selects the entire function. If you have functions inside another, you gotta indent everything (won't work for just a foo inside another).

ipywidget interactive plot in Html presentations ipython 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."

Jupyter/IPython: how to get results in traditional mathematical notation?

I have been using WxMaxima for my symbolic calculations for a while now. The good thing about WxMaxima is that you can get formatted outputs right in the program and then export them to LaTeX format with a click of the mouse.
Now I want to try the Jupyter/Ipython plus sympy for multiple reasons. I know how to use display(Math(r' some LaTeX math here ')) but what I want is to have the result/output of a cell in a nice mathematical form; something like the TraditionalForm[] command in Mathematica.
I would appreciate if you could help me know if/how I can get that right in a Jupyter notebook?
I think I found the proper solution and it is a sympy feature rather than Jupyter/IPython one. As explained here:
If all you want is the best pretty printing, use the init_printing() function. This will automatically enable the best printer available in your environment.
and
In the [Jupyter/]IPython notebook, it will use MathJax to render LATEX.
Then one can right click on the output and select Show Math As > Tex commands:
to get the LaTeX output.
P.S. A more proper formatting can be achieved via galgebra library. I will look into that and add it here later.

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

Mathematica-like (LaTeX) typesetting for own CAS application

As I am using Mathematica a lot I got the idea to write a small and free CAS which just exposes a very small subset of necessary functions and packages to be used and I want to present the results in an appropriate way to the user like Mathematica does (ignore the Facebook logo in the background :D ):
My first idea was to create LaTeX code in the background and to pdflatex the source and include the PDF then in the view... however this seems way to much overkill! I want to write this CAS either in C++ or C# and I want to know if there are any recommended solutions to output nice formula like that.
My first thought was a "real-time formula editing view" but it would be ok to have an input box to enter the commands and formulas and the upper view just to be uneditable output.
A few ways come to my mind.
Use LaTeX behind the scenes to typeset equations, as you say. Again, Cadabra does this.
Use TeXmacs as the front end. Cadabra does this.
Use MathJax. This is a javascript framework which renders TeX equations to images or MathML. It's very easy to use it if you have a HTML view in your UI toolkit. MathJax is used in the sister site MathOverflow, for example.
I find the route 3 is the most attractive.
For calling LaTeX in the background, don't use pdflatex, but use the non-PDF latex to produce a DVI file, and convert it then to PNG with dvipng.
Have a look at the preview package or the standalone class to get the output in the right size (i.e. only the formula, not a whole page).

Resources