Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Jupyter Notebooks have an awesome feature where they can render charts and graphs from packages like Bokeh and Matplotlib.
Here's the code snippet from the video:
from ipywidgets import interact
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
output_notebook()
x = np.linspace(0, 2*np.pi, 2000)
y = np.sin(x)
p = figure(title='simple line example', plot_height=300, plot_width=600, y_range=(-5,5))
r = p.line(x, y, color='#2222aa', line_width=3)
def update(f, w=1, A=1, phi=0):
if f == "sin": func = np.sin
elif f == "cos": func = np.cos
elif f == "tan": func = np.tan
r.data_source.data['y'] = A * func(w * x + phi)
push_notebook()
show(p, notebook_handle=True)
interact(update, f=["sin", "cos", "tan"], w=(0,100), A=(1,5), phi=(0, 20, 0.1))
You'll also need to run jupyter nbextension enable --py --sys-prefix widgetsnbextension
in your terminal.
Sharing your notebooks
Now before I can do anything
with charts and graphs,
0:00
I need to get a couple
of things installed.
0:02
Luckily, we can do this even
inside of our notebook.
0:04
So I'm gonna add a new cell and
0:07
I'm gonna type !pip3 install numpy and
bokeh.
0:10
Now if you have Python 3 installed
to where you can just do pip,
0:16
then you want to go ahead and
do that as well.
0:19
I'm gonna go ahead and
execute that cell, and
0:22
l can see up here that
this is marked as busy.
0:24
And l can see output showing up
in here in this little star here.
0:27
All of these things tell
me that it's busy, and
0:30
what's cool is if you
hover over this thing,
0:33
it'll tell you whether or not the kernel
is idle or whether the kernel is busy.
0:35
If the kernel was still busy,
it would say that it was busy.
0:38
So right now it's installing NumPy and
Bokeh, or it was.
0:42
NumPy is a package for working with
numerical data, for data science.
0:46
And Bokeh is one for
generating interactive charts.
0:50
Now, once this all wraps up,
we can add our next block of code, or
0:52
I could change this one.
0:57
I'm gonna add a new one though,
just to keep that bit of history in there.
0:58
So I've got a block of code,
you can check the teacher's notes for it.
1:02
I'm gonna copy and
paste it from my notes, though.
1:06
So copy that, and
then I'm going to come over here and
1:11
paste that in, all right.
1:16
So it's not super important
what all's going on here, but
1:19
this is using NumPy and
some widgets to generate a sine wave.
1:22
So I'm gonna go ahead and I'm gonna
execute this with control and enter.
1:27
And I don't get any interesting feedback,
I just get this function main update.
1:32
Now the reason that I get that is
because I haven't enabled JavaScript
1:38
inside of our notebook.
1:41
You have to tell Jupyter notebooks that
you're okay with it executing JavaScript
1:43
inside the notebook.
1:47
Now I can't turn this feature on
from inside the notebook itself.
1:48
So I'm gonna hop over to my terminal,
I'm going to stop the notebook server.
1:52
And then I'm going to run the code that
I need to do so that I can execute this.
1:59
So I stopped the server by
pressing Control and C.
2:04
On some systems you may have to do
Control and D or Control and Z.
2:07
Just play with it, you'll find
which one works for your system.
2:11
So now I need to enable the extension,
2:14
the extension that I want to
enable is the widgets extension.
2:16
And we want it to be enable for Python.
2:19
So we're gonna type the following
command into the terminal.
2:20
So here's the command,
2:23
it is jupyter nbextension enable ==py
2:26
==sys-prefix widgetsnbextension.
2:32
So nbextension enable --py
--sys-prefix widgetsnbextension.
2:40
This is in the teacher's notes,
as well, if you just wanna copy and
2:46
paste it out of there.
2:49
You'll see that it's validated and
it's okay.
2:51
And then we're going to run
Jupyter notebook again.
2:53
And when we do, it will again open up,
yeah go ahead and leave.
2:57
>> It'll open up our thing and
we will open up our notebook.
3:00
And then, it looks like I'm gonna
need to paste that code back in.
3:06
And then I will run it one more time,
control Enter.
3:11
And down below now,
3:15
you will see that we have a wonderful
little thing here where we have waves.
3:16
And if we were to change the values,
it will change the wave right away.
3:21
And we can change which wave type it
is whether it's a sine or cos or a tan.
3:28
And we can just kinda play
around with this stuff.
3:33
And then what's cool is we have
over here tools for panning and
3:36
zooming an stuff like that as well.
3:38
So it's pretty cool, we have a bunch
of widgets and we have a wave, and
3:40
if we change the values
we get a different wave.
3:43
And it's all right here in the notebook,
we don't have to use any extra programs or
3:45
anything like that to see what's going on.
3:48
It's really neat, you can do a lot more
with extensions that what I've shown you.
3:50
There are others available online and
3:54
with them you can do things like use
Python's turtle module to draw graphics.
3:56
Explore other data visualization
libraries and even use other languages.
3:59
We'll explore the other languages
a tiny bit in the next video.
4:03
But know that there's a lot more out
there for you to explore and discover.
4:06
If you'd like to share your
notebooks with friends and
4:10
coworkers, check the teacher's notes for
ways to do that.
4:11
It can be as easy as putting
the notebook on GitHub or
4:13
Azure, using Project Jupyter's
official notebook viewer.
4:16
Or you can export the notebook to HTML and
host it on your own server or
4:19
another service that'll
host static websites.
4:22
Again, check the teacher's notes for
more information.
4:24
All right, let's see how they use Jupyter
notebooks with more than just Python.
4:27
I'm gonna have to take
some notes here myself.
4:30
You need to sign up for Treehouse in order to download course files.
Sign up