Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create python classes in Jupyter Notebook

I've tried to created and use classes in jupyter notebook. But it seems it doesn't work And I've tried this :

def pxlocal(line, cell):
    ip = get_ipython()
    ip.run_cell_magic("px", line, cell)
    ip.run_cell(cell)
ip.register_magic_function(pxlocal, "cell")    

And in different cell:

%%pxlocal class MyClass(object):

But when I run those two cells it gave me this error:

ERROR:root:Cell magic `%%px` not found.
In [11]:

What am I doing wrong?

like image 259
Nebi M Aydin Avatar asked May 08 '17 20:05

Nebi M Aydin


People also ask

How do I run a Python class in a Jupyter Notebook?

Run the notebook by typing jupyter notebook into command line. In the web application that opens, navigate to the folder containing the files you downloaded, and open 'Classes in Python.

How do I create a Python program in Jupyter Notebook?

To create a new notebook, go to New and select Notebook - Python 2. If you have other Jupyter Notebooks on your system that you want to use, you can click Upload and navigate to that particular file. Notebooks currently running will have a green icon, while non-running ones will be grey.

Can we create module in Jupyter Notebook?

In order to create a module in Jupyter Lab, first create a new notebook. Rename the notebook (e.g. 'module1. ipynb') and copy paste the code in the notebook. Click 'File', 'Download as' and 'Python'

Can we practice Python on Jupyter Notebook?

Jupyter Notebook is an environment that we can use to experiment with Python interactively . It allows you to share live Python code with others .


1 Answers

There is no problem with defining a class in a different cell. Just make sure you define the class in a cell that appears before the cell that uses it.

like image 196
eli-bd Avatar answered Oct 06 '22 04:10

eli-bd