import papermill as pm
from subprocess import Popen
import os

root_path = ''
with open('../paths.config', 'r') as f:
    root_path = f.read()
    root_path = root_path.strip()
    f.close()

os.chdir(os.path.join('cancer_img', 'wsi'))

def run_with_pm(input_file: str, id: str) -> None:
    output_file = os.path.join(id, f'{id}_output.ipynb')
    pm.execute_notebook( os.path.join('wsi_nb.ipynb'), output_file, parameters = dict(file = rf'{input_file}'))

    conversion_cmd = f"python3 -m jupyter nbconvert --TemplateExporter.exclude_input=True --to html {output_file}"

    process = Popen(conversion_cmd, shell=True)

    process.wait()

    move_to_templates = f"mv {output_file.split('.')[0] + '.html'} templates"

    process = Popen(move_to_templates, shell=True)

    process.wait()

    return True
    
def generate_output_images(input_file: str, id: str) -> None:
    import slideio
    import matplotlib.pyplot as plt

    slide = slideio.open_slide(file, "SVS")
    scene1 = slide.get_scene(0)
    image1 = scene1.read_block((0,0,1000,1000))
    scene2  = slide.get_scene(0)
    scene2.name, scene2.rect, scene2.num_channels, scene2.resolution
    image2 = scene2.read_block(size=(500,0))
    image = scene2.read_block((5000, 5000, 5000, 5000), size=(500,0))
    
    fig, axarr = plt.subplots(1,3, figsize=(9,3))
    axarr[0].imshow(image1)
    axarr[1].imshow(image2)
    axarr[2].imshow(image)    
    
    os.chdir(root_path)
    print(f'sending out the file with id:{id}')
    plt.savefig(os.path.join('static', f'{id}_output.png'))
    
    return True




    
    
    
    

    
    


