Run ipynb from python file keeping imports and variables

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)

i also reffering to check this also please: How can I set up default startup commands in iPython notebooks? - Stack Overflow sangeet

You might also try something like importnb or ipynb which offer other approaches for importing python notebooks.

Another suggestion, maybe more helpful for those finding this later, is that nbformat makes accessing & extracting of the code cells of the notebooks json a bit more straighforward.
Because it is part of the Jupyter machinery, it’s already present wherever you are using notebooks.
There’s several stubs of code examples in this forum if you search ‘nbformat’, or you can look at this post and the links below it. This code illustrates the ease with which you can use it to subset notebooks.