Giter Club home page Giter Club logo

Comments (5)

darabos avatar darabos commented on July 28, 2024 5

A simpler solution that I would be 100% happy with would be just to ignore magics. Currently I get:

File "<unknown>", line 15
    %matplotlib inline
    ^
SyntaxError: invalid syntax

from ipynb.

xgdgsc avatar xgdgsc commented on July 28, 2024 1

I would also like to just ignore magic commands. Usually I only use %matplotlib inline and %timeit.

from ipynb.

AEDWIP avatar AEDWIP commented on July 28, 2024

In my use case I only want to import function definitions. I would prefer that all other code cells be skipped

I am happy to define my functions in separated cells to make it easier for the parser

I use %matplotlib and %timeit a lot.

from ipynb.

panaali avatar panaali commented on July 28, 2024

I also get the same error when using "!"
for example: "!pip"

from ipynb.

cmrfrd avatar cmrfrd commented on July 28, 2024

So this isn't a direct solution but maybe a step forward. Since magics are usually applied at the "cell" level it might be worth considering importing "cells" instead of just functions. If that's the case then you can use something like this

from IPython.lib.kernel import get_connection_file
from jupyter_client import BlockingKernelClient

def import_cell(notebook_path, cell_number):

    ## Read notebook
    notebook_file = open(notebook_path)
    nb = nbformat.read(notebook_file,as_version=4)

    ## Creat kernel client on current
    ## running kernal
    client = BlockingKernelClient()
    client.load_connection_file(get_connection_file())
    client.start_channels()

    ## Execute code cell
    nb_code_cells = [c for c in nb.cells if c["cell_type"] == "code"]
    client.execute(code_cells[cell_number].source)

import_cell("path/to/notebook.ipynb", 0)

To "import" (really it just executes the cell in your current ipython kernel). But if users want to import cells by cell tag they could do something like

from ipynb.fs.cells.my_notebook import my_cell_tag

Where the cell tag could be searched for in the metadata across the notebook

from ipynb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.