Load plain text inline in Markdown in Jupyter - python

I know you can 'import' text or code in Jupyter notebook cells with %load file_name.ext but that pastes the entirety in the cell. Is there a way to load a blog of text inside a Markdown cell at the desired location?
What I'm looking for is this:
This is some custom text from my current notebook and because I share the same example across multiple notebooks, I'd like to re-use it and not copy-paste it everywhere:
<<<insert contents of some_file.md>>>
And then I can continue with a text that's only visible in this notebook.
That way I do not have to repeat myself and have a single place where I can edit these shared bits of text (or code).

Related

How to remove or change the header of jupyter notebooks pdf in Visual Studio Code?

I am in Visual Studio Code and I have created the following jupyter notebook:
I would like to know how to remove or modify the pdf header (circled in red below) produced when I export this notebook to pdf with VSCode.
I know this is a similar header to a latex document produced with \maketitle, but I don't know how to remove it or how to change the title, add authors or change the date. I would also like to know how to reduce the margins of the pdf produced (in my latex document I use \geometry for).
Thanks for your help :
In fact, the best way to convert jupyter files to PDF is not through vsocd or notepad. We can open jupyter files through Chrome browser, and then convert them to PDF by printing, so that no title can be adjusted, and row spacing can be adjusted in print.
I didn't find a satisfying solution: you would have to edit the notebook's metadata (in JSON).
For now, I:
export the notebook as LaTeX source (e.g. jupyter-nbconvert --to latex my_notebook.ipynb)
edit the generated .tex file to adjust title and author.
compile it to pdf (pdflatex or your favorite tool).

Is it possible to redirect cell output in jupyter

I am using jupyter and jupyter-nbconvert to create a html presentation. However, I have some cells that produce an output image that I want to share on a separate slide. Is it possible to redirect the output of one cell to its own slide?
You might want to consider using Damian Vila's Jupyter extension RISE. It provides some of the control you need for how cells are displayed in slides.
It is flagged by the latest Jupyter (3.6) as possibly not compatible, but I've seen no problems using it so far.

Images and Widgets don't appear in html after converting Jupyter notebook - Python

I'm using Jupyter notebook to write up my report using Markdown and Python to make widgets.
On the Jupyter notebook, when I click on "file", then "Download as" HTML(.html), it converts my notebook.
In the html, I noticed that none of my widgets appear, and any images I've attached by markdown as <img src="myimagehere.png"> also disappear.
Only the actual code, " appears, not the image itself.
P.S. I also just noticed that the markdown mathematical notation like Latex suddenly appears as only code in html. So three problems in one, wonderful.
Been struggling to find an answer/solution. Can anyone please help? Many thanks.

How to export and preserve linked Jupyter notebooks?

I have multiple Jupyter notebooks that are linked to one another such that Notebook1.ipydb contains a link to Notebook2.ipydb with the markdown [Notebook2](Notebook2.ipynb) and vice versa.
When exporting all notebooks to HTML via nbconvert, the link to Notebook2.ipynb is preserved. I would like to change that link to the exported Notebook2.html so the linked HTML files function as a static website.
I tried to detect if I was running in iPython using get_ipython().__class__.__name__, but it executes this code before converting to HTML.
Is there a way to detect a static file to conditionally render the right markdown? Is there another way to preserve linked notebooks?
There's only really two options. One is to link to Notebook2.html in the first place and the other is to create a custom preprocessor for nbconvert.
from nbconvert.preprocessors import Preprocessor
import re
class CustomPreprocessor(Preprocessor):
def preprocess_cell(self, cell, resources, index):
if 'source' in cell and cell.cell_type == "markdown":
cell.source = re.sub(r"\[(.*)\]\(\1\.ipynb\)",r"[\1](\1.html)",cell.source)
return cell, resources
Save this to a file, then add to your nbconvert config file (located at ~/.jupyter/jupyter_nbconvert_config.py or can be generated using the command jupyter nbconvert --generate-config) the line:
c.HTMLExporter.preprocessors = ['CustomPreprocessor.CustomPreprocessor']
This assumes that the custom preprocessor file is named CustomPreprocessor and is located in the same directory as the files you're trying to convert. You could also properly install it as a module too.

IPython notebook read string from raw text cell

I have a raw text cell in my IPython notebook project.
Is there a way to get the text as a string with a build in function or something similar?
My (possibly unsatisfactory) answer is in two parts. This is based on a personal investigation of iPython structures, and it's entirely possible I've missed something that more directly answers the question.
Current session
The raw text for code cells entered during the current session is available within a notebook using the list In.
So the raw text of the current cell can be returned by the following expression within the cell:
In[len(In)-1]
For example, evaluating a cell containing this code:
print "hello world"
three = 1+2
In[len(In)-1]
yields this corresponding Out[] value:
u'print "hello world"\nthree = 1+2\nIn[len(In)-1]'
So, within an active notebook session, you can access the raw text of cell as In[n], where n is the displayed index of the required cell.
But if the cell was entered during a previous Notebook session, which has subsequently been closed and reopened, that no longer works. Also, only code cells seem to be included in the In array.
Also, this doesn't work for non-code cells, so wouldn't work for a raw text cell.
Cells from saved notebook sessions
In my research, the only way I could uncover to get the raw text from previous sessions was to read the original notebook file. There is a documentation page Importing IPython Notebooks as Modules describing how to do this. The key code is in In[4]:
# load the notebook object
with io.open(path, 'r', encoding='utf-8') as f:
nb = current.read(f, 'json')
where current is an instance of the API described at Module: nbformat.current.
The notebook object returned is accessed as a nested dictionary and list structure, e.g.:
for cell in nb.worksheets[0].cells:
...
The cell objects thus enumerated have two key fields for the purpose of this question:
cell.cell_type is the type of the cell ("code", "markdown", "raw", etc.).
cell.input is the raw text content of the cell as a list of strings, with an entry for each line of text.
Much of this can be seen by looking at the JSON data that constitutes a saved iPython notebook.
Apart from the "prompt number" fields in a notebook, which seem to change whenever the field is re-evaluated, I could find no way to create a stable reference to a notebook cell.
Conclusion
I couldn't find an easy answer to the original question. What I found is covered above. Without knowing the motivation behind the original question, I can't know if it's enough.
What I looked for, but was unable to identify, was a way to reference the current notebook that can be used from within the notebook itself (e.g. via a function like get_ipython()). That doesn't mean it doesn't exist.
The other missing piece in my response is any kind of stable way to refer to a specific cell. (e.g. Looking at the notebook file format, raw text cells consist solely of a cell type ("raw") and the raw text itself, though it appears that cell metadata might also be included.) This suggests the only way to directly reference a cell is through its position in the notebook, but that is subject too change when the notebook is edited.
(Researched and answered as part of the Oxford participation in http://aaronswartzhackathon.org)
I am not allowed to comment due to my lack of reputation so I will just post as answer an update to Graham Klyne's answer, in case someone else stumble into this. (Ipython has no updated documentation yet to date)
Use nbformat instead of Ipython.nbformat.current
The worksheets attribute is gone so use cells directly.
I have an example of how the updated code will look like:
https://github.com/ldiary/marigoso/blob/master/marigoso/NotebookImport.py

Categories

Resources