import json


def read_notebook(file, store_markdown= False):
    """Reads a notebook file and returns the code"""

    code = json.load(open(file))
    # file = getBaseNameNoExt(file)
    py_file = "" # open(f"{file}.py", "w+")

    for cell in code['cells']:
        if cell['cell_type'] == 'code':
            for line in cell['source']:
                py_file += line # py_file.write(line)
            py_file += "\n" # py_file.write("\n")
        elif cell['cell_type'] == 'markdown' and store_markdown:
            py_file += "\n" # py_file.write("\n")
        
            for line in cell['source']:
                if line and line[0] == "#":
                    py_file += line # py_file.write(line)
            py_file += "\n" # py_file.write("\n")
    # py_file.close()
    
    return py_file