Plotting in Python Notebook (Docs) - jupyter-notebook

Attempting to go through the pandas doc at http://pandas.pydata.org/pandas-docs/stable/visualization.html#basic-plotting-plot I get an error :
NameError: name 'Series' is not defined
I can import pandas and supply 'pandas.Series', but I want to know how to set up notebook, as in the docs, so that it's already included.

You can customize Ipython notebook profile file ipython_config.py typically located in ~/.ipython/profile_default/ by adding something like :
c.InteractiveShellApp.exec_lines = [
'import pandas',
]
You can read the documentation for ipython latest version here

Related

Running old jupyter notebooks

I am trying to use a Jupyter notebook that was written by somebody else around 8 years ago. And the contents are written in Python2 and not Python3 that I am using. I am new to it and I can't figure out the reason for the syntax error.
The following are the contents of the first cells that is giving a syntax error.
`# Always run this first
# NOTE: Do not define new basic variables in this notebook;
# define them in Variables_Q.ipynb. Use this notebook
# to define new expressions built from those variables.
from __future__ import division # This needs to be here, even though it's in Variables_Q.ipynb
import sys
import os
sys.path.append(os.path.join(os.path.dirname('/home/khushal/Desktop/Python/Git Repositories/PostNewtonian/PNTerms'
)) # Look for modules in directory above this one
exec(open('../Utilities/ExecNotebook.ipy').read(),{})
from ExecNotebook.ipy import execnotebook
try: execnotebook(VariablesNotebook)
except: execnotebook('Variables_Q.ipynb')`
The error is
File "/tmp/ipykernel_31372/53020713.py", line 11
exec(open('../Utilities/ExecNotebook.ipy').read(),{})
^
SyntaxError: invalid syntax
I tried to use a 2to3 jupter notebook converter but it didn't change the jupyter notebook at all and it is giving the same result.

Unable to import Python file in VSCode Julia notebook

I'm trying to import a Python file containing a single function into my Julia notebook in VSCode.
The file is called helloPython.py and contains a single function called helloFromPython.
It contains the following code.
def helloFromPython():
print("Hello Julia, it's me Python!")
It is located in the same directory as my notebook file.
My first cell looks as follows.
import Pkg
Pkg.add("PyCall")
Pkg.build("PyCall")
using PyCall
Importing inbuilt Python functions works as expected, e.g.
math = pyimport("math")
math.sin(math.pi / 4)
Defining and calling Python code on the fly also works as expected, for example
begin
py"""
def f(x):
return x**3
"""
f = py"f"
end
f(2) # prints 8
However, importing my local Python file does not work. My cell looks as follows.
pushfirst!(PyVector(pyimport("sys")."path"), "")
x = pyimport("helloPython")
x.helloFromPython()
There is no error message. The cell executes successfully and that's it.
I've tried the same using the inbuild terminal in VSCode starting the Julia command prompt and it works as expected.
What am I doing wrong here?
Edit: #alex338207 had a good idea here, so I'm updating my question.
Writing to a file works. Here is a minimal working example. The file helloPython.py contains the following code.
def helloFromPython():
f = open("test.txt", "a")
f.write("Hello Julia!")
f.close()
Calling the above in VSCode as describe above, works as expected. A file is created with the provided content "Hello Julia!".
However, returning stuff doesn't work. Here is another example.
def helloFromPython():
return "Hello Julia!"
Calling the above yields to the output nothing.
I've updated my issue on Github.

How to Hide Code cells When converting a Jupyter Notebook into Sphinx Doc using nbsphinx

I'm new to Sphinx documentation, and I am trying to convert a Jupyter Notebook into Sphinx docs using Nbsphinx. Especially, I want to hide the code cells (not their outputs), but I could not make it using templates that I found for Jupyter Nbconvert.
I expect the outputs from converted notebook on the Sphinx documentation, but no input.
I added the following code to the first cell and it works well.
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)'

removing certain cells via tags fails due to wrong type

I want to hide certain parts of a jupyter notebook and came across tags which can achieve this. I've tagged the cells with remove_cell in my notebook an tried to run
$ jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="{'remove_cell'}"
however I always get the following error:
traitlets.traitlets.TraitError: The 'remove_input_tags' trait of a TagRemovePreprocessor instance must be a set, but a value of type 'unicode' (i.e. u'{remove_cell}') was specified.
I've tried to change the "{'remove_cell'}" to various format, e.g. {'remove_cell'} etc with the same result. Any help would be appreciated
According to nbconvert documentation it must be done as you have specified. But there seems to be some bug in command line parsing traitlets API, used internally by jupyter nbconvert. So i tried a slightly different approach of specifying Configuration in jupyter_nbconvert_config.py file.
Steps:
jupyter nbconvert --generate-config
This will generate default ~/.jupyter/jupyter_nbconvert_config.py.
Edit the configuration file and specify your configuration, in this case
c.TagRemovePreprocessor.remove_input_tags = set(['remove_cell'])
Run jupyter nbconvert test.ipynb This will remove the tagged cells and convert it to default HTML page.
I got this to work without editing the config file by using square brackets, which match the format of the tag in the cell metadata, instead of curly braces.
So, in my cell metadata I have this:
{
"tags": ["remove_cell"]
}
and on the command line I used:
jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="['remove_cell']"
which successfully removed any cells with this tag from the HTML output.
nbconvert did not hide the cells with tags while also executing the notebook, and this is the most useful example I found here https://github.com/jupyter/nbconvert/issues/1300:
So the underlying issue is that the preprocessors being added on your behalf are un-ordered, so by unlikely chance the --execute turning on the ExecutePreprocessor is getting applied after the TagRemovePreprocessor. To fix this you can set the preprocessors to use in an explicit order by doing:
jupyter nbconvert notebooks/demo_notebook.ipynb \
--TagRemovePreprocessor.remove_input_tags='{"remove-input"}' \
--TagRemovePreprocessor.remove_all_outputs_tags='{"remove-output"}' \
--Exporter.preprocessors='["nbconvert.preprocessors.ExecutePreprocessor","nbconvert.preprocessors.TagRemovePreprocessor"]' \
--to notebook

%edit magic command not working in jupyter notebook

I can't work out how to get the %edit command working in Jupyter Notebook.
I type
%edit
and it returns with
IPython will make a temporary file named: /var/folders/dk/.../ipython_edit_JbS9ZC.py
My %EDITOR environment variable is
'EDITOR': '/usr/local/bin/subl -w'
I must be doing something wrong.
This feature issue appears to be well-known : https://github.com/ipython/ipython/issues/5879
A possible solution might be to use : Edit IPython cell in an external editor
Or you can embed the notebook in emacs (I don't use emacs) : https://github.com/tkf/emacs-ipython-notebook

Resources