Home > Software design >  Run ipynb from python file keeping imports and variables
Run ipynb from python file keeping imports and variables

Time:03-02

I'm trying to create a project in ipynb file which is accessed and executed by a python script for ease of creating a terminal based interface. The ipynb is pretty much complete, but the issue arises when trying to execute two code cells from the script.

I'm using this code to extract the code cells from the ipynb file:

# Initializing AI iPython Notebook cells and definitions
rawFile = open('Keras Implementation new.ipynb').read()
cells = json.loads(rawFile)
cells = cells["cells"]

codeCells = []
for i in cells:
    if i["cell_type"] == "code":
        codeCells.append(i["source"])

cellDefinitionsRaw = open('Cell Definitions.json').read()
cellDefinitions = json.loads(cellDefinitionsRaw)

I'm using this code to test execution of two cells in a row:

def executeCell(cell):
    for i in cell:
        exec(i)

executeCell(codeCells[0])
executeCell(codeCells[1])

These two code cells are executed as a test:

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

from sklearn.preprocessing import MinMaxScaler

from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from sklearn.metrics import mean_squared_error
dataset = pd.DataFrame(pd.read_csv('AAAU.csv'))
dataset.head()

Here I'm receiving error that pd is not defined. Is there a way to carry imports over from the exec statements other than including them in the base script? Because I think the issue might carry over to variables too

Edit (Answer):

I re-checked the docs and found out that exec call can be modified to use the global variables using this:

exec(codeCells[i], globals())

And all the code lines in a cell can be appended to a single string using this code:

codeCells = []
for i in cells:
    if i["cell_type"] == "code":
        code = ""
        for j in i["source"]:
            code  = j
        codeCells.append(code)

CodePudding user response:

I re-checked the docs and found out that exec call can be modified to use the global variables using this:

exec(codeCells[i], globals())

And all the code lines in a cell can be appended to a single string using this code:

codeCells = []
for i in cells:
    if i["cell_type"] == "code":
        code = ""
        for j in i["source"]:
            code  = j
        codeCells.append(code)

CodePudding user response:

instead of this :

def executeCell(cell): for i in cell: exec(i)

executeCell(codeCells[0]) executeCell(codeCells[1])

try this: exec(codeCells[0]) exec(codeCells[1])

  • Related