0

Very new to this. I'm in Jupyter Notebook, following instructions to make interactive visualizations. I'm using the following code, taken directly from the site:

from IPython.html.widgets import *
t = arange(0.0, 1.0, 0.01)

def pltsin(f):
    plt.plot(x,sin(2*pi*t*f))
    plt.show()
    
interact(pltsin, f=(1,10,0.1))

When I try this, I get the following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [3], in <cell line: 10>()
      6     print(text.value)
      8 text.on_submit(handle_submit)
---> 10 from IPython.html.widgets import *
     11 t = arange(0.0, 1.0, 0.01)
     13 def pltsin(f):

ModuleNotFoundError: No module named 'IPython.html'

What's the problem here? Is there something I've failed to install? Thanks in advance for the assistance.

NKOrsay
  • 3
  • 3
  • Did you install it? `pip install ipython` – Fiddling Bits Aug 16 '22 at 19:44
  • 2
    That code looks out of date. In current IPython, the module is `ipywidgets`: so try `from ipywidgets import *`, or maybe better, import just what you need, i.e. `from ipywidgets import interact`. – slothrop Aug 16 '22 at 19:50
  • When I run this it's returning a list of "Requirement already satisfied". – NKOrsay Aug 16 '22 at 19:51
  • @slothrop it looks like that line worked! Now it's giving me trouble on the `arange` command. – NKOrsay Aug 16 '22 at 19:55
  • That's a numpy function, so try `import numpy as np` then refer to the function as `np.arange`. – slothrop Aug 16 '22 at 19:56
  • 1
    General note: Python, and particularly libraries around data handling and visualisation, evolve over time, so it's best to use tutorials which were written or updated recently. This one is nearly 7 years old...however, the missing numpy import can't be blamed on that :) – slothrop Aug 16 '22 at 19:59
  • I'm going to post an 'answer' so I can include code. I really am just trying to point you to updated documentation and methods that require variations on what you have. – Wayne Aug 17 '22 at 20:03

2 Answers2

0

This is a companion to my comment under the OP so that I can add code.
Here is an updated version of the code you have that will work. It is largely based on here which notes that now the ipywidgets documentation features interactive and not interact with the matplotlib example. (I was unsure about the x in the Domino Labs code you linked and so you may want to look at what I did in regards to that.)

from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 1.0, 0.01)


def pltsin(f):
    plt.plot(t,np.sin(2*np.pi*t*f))
    plt.show()

interactive_plot = interactive(pltsin, f=(1,10,0.1))
interactive_plot

Try it out in your browser without installing anything.

To try it out in a session backed by MyBinder where the environment already has ipywidgets and numpy installed, click here and then choose Run > Run All Cells from the main menu to run the code and see the interactive plot.

Wayne
  • 6,607
  • 8
  • 36
  • 93
-1

Run ! pip install IPython within the jupyter notebook.

The ! tells the notebook to run the command in bash, just make sure the pip you are using is the same interpreter the notebook is using

  • 1
    When I run this it's returning a list of "Requirement already satisfied". How do I ensure the pip is the same interpreter? – NKOrsay Aug 16 '22 at 19:51
  • @Osei-Asante, please no longer suggest using an exclamation for use with `pip` or `conda`. Magic commands were added in the last few years to deal with the issues that can happen when an exclamation point is used to attempt installation inside a Jupyter notebook. See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about the modern magics **that insure installation occurs in the environment backing the notebook**. – Wayne Aug 17 '22 at 19:34