DownloadPane

After OptiPass has completed the last optimization run the GUI creates an instance of this class and saves it in the Download tab of the top level display.

Check the output panel to see which plots were created and to enable the net benefit plot if there is one.

The pane also has a form to allow the user to enter the name of the download file, the format for the figures, and a button to click when they are ready to download the data.

Parameters:
  • outputs

    the OutputPane object containing data tables and plots

Source code in src/tidegates/widgets.py
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
def __init__(self, outputs):
    """
    Display a set of checkboxes for the user to select what sort of data to
    include in a zip file.  If the gate table is not empty enable table downloads.
    Check the output panel to see which plots were created and to enable the
    net benefit plot if there is one.

    The pane also has a form to allow the user to enter the name of the download
    file, the format for the figures, and a button to click when they are ready
    to download the data.

    Arguments:
      outputs:  the OutputPane object containing data tables and plots
    """
    super(DownloadPane, self).__init__()
    self.outputs = outputs
    self.folder_name = self._make_folder_name()

    self.grid = pn.GridBox(ncols=2)
    self.boxes = { }
    for x in [self.NB, self.BS, self.IT, self.BD]:
        b = pn.widgets.Checkbox(name=x, styles=box_styles, stylesheets=[box_style_sheet])
        if x in [self.NB, self.IT]:
            b.disabled = True
            b.value = False
        else:
            b.value = True
        self.boxes[x] = b
        self.grid.objects.append(b)

    self.filename_input = pn.widgets.TextInput(
        name = '', 
        value = self.folder_name,
    )

    self.image_type = pn.widgets.RadioBoxGroup(name='IFF', options=['HTML','PDF','PNG','JPEG'], inline=True)

    self.make_archive_button = pn.widgets.Button(name='Create Output Folder', stylesheets=[button_style_sheet])
    self.make_archive_button.on_click(self._archive_cb)

    self.append(pn.pane.HTML('<h3>Save Outputs</h3>', styles=header_styles))
    if outputs.gate_count > 0:
        self.append(pn.pane.HTML('<b>Items to Include in the Output Folder:</b>')),
        self.append(self.grid)
        self.append(pn.Row(
            pn.pane.HTML('<b>Image File Format:</b>'),
            self.image_type,
            margin=(20,0,0,0),
        ))
        self.append(pn.Row(
            pn.pane.HTML('<b>Output Folder Name:</b>'),
            self.filename_input,
            margin=(20,0,0,0),
        ))
        self.append(self.make_archive_button)
        self.append(pn.pane.HTML('<p>placeholder</p>', visible=False))

    # if there are figures at least one of them is an individual target, so enable
    # that option; if there is a net benefit figure it's the first figure, enable it
    # if it's there

    if len(outputs.op.display_figures) > 0:
        if outputs.op.display_figures[0][0] == 'Net':
            self.boxes[self.NB].value = True
            self.boxes[self.NB].disabled = False
        self.boxes[self.IT].value = True
        self.boxes[self.IT].disabled = False

_archive_cb(e)

Function called when the user clicks the Download button. Create the output folder and compress it. When the archive is ready, display a FileDownload widget with a button that starts the download.

src/tidegates/widgets.py
759
760
761
762
763
764
765
766
767
768
769
770
771
772
def _archive_cb(self, e):
    """
    Function called when the user clicks the Download button.  Create the output
    folder and compress it.  When the archive is ready, display a FileDownload
    widget with a button that starts the download.
    """
    if not any([x.value for x in self.boxes.values()]):
        return
    self.loading = True
    base = self._make_archive_dir()
    self._save_files(base)
    p = make_archive(base, 'zip', base)
    self.loading = False
    self[-1] = pn.widgets.FileDownload(file=p, filename=self.filename+'.zip', stylesheets=[button_style_sheet])

_make_archive_dir()

Create an empty directory for the download, using the name in the form.

src/tidegates/widgets.py
774
775
776
777
778
779
780
781
782
783
def _make_archive_dir(self):
    """
    Create an empty directory for the download, using the name in the form.
    """
    self.filename = self.filename_input.value_input or self.filename_input.value
    archive_dir = Path.cwd() / 'tmp' / self.filename
    if Path.exists(archive_dir):
        rmtree(archive_dir)
    Path.mkdir(archive_dir)
    return archive_dir

_make_folder_name()

Use the region names, target names, and budget range to create the default name of the zip file.

src/tidegates/widgets.py
745
746
747
748
749
750
751
752
753
754
755
756
757
def _make_folder_name(self):
    """
    Use the region names, target names, and budget range to create the default name of the zip file.
    """
    parts = [s[:3] for s in self.outputs.op.regions]
    lst = [t.abbrev for t in self.outputs.op.targets]
    if self.outputs.op.weighted:
        lst = [f'{lst[i]}x{self.outputs.op.weights[i]}' for i in range(len(lst))]
    parts.extend(lst)
    parts.append(OP.format_budget_amount(self.outputs.op.budget_max)[1:])
    if any(t.infra for t in self.outputs.op.targets):
        parts.append(self.outputs.op.climate[0])
    return '_'.join(parts)

_save_files(loc)

Write the tables and figures to the download directory.

Parameters:
  • loc

    the path to the directory.

src/tidegates/widgets.py
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
def _save_files(self, loc):
    """
    Write the tables and figures to the download directory.

    Arguments:
      loc:  the path to the directory.
    """
    figures = self.outputs.op.display_figures if self.image_type.value == 'HTML' else self.outputs.op.download_figures
    for name, fig in figures:
        if name == 'Net' and not self.boxes[self.NB].value:
            continue
        if name != 'Net' and not self.boxes[self.IT].value:
            continue
        if self.image_type.value == 'HTML':
            savehtml(fig, filename=loc/f'{name}.html')
        else:
            ext = self.image_type.value.lower()
            fn = loc/f'{name}.{ext}'
            fig.savefig(fn, bbox_inches='tight')
    if self.boxes[self.BS].value:
        df = self.outputs.budget_table.drop(['gates'], axis=1)
        df.to_csv(
            loc/'budget_summary.csv', 
            index=False,
            float_format=lambda n: round(n,2)
        )
    if self.boxes[self.BD].value:
        self.outputs.gate_table.to_csv(
            loc/'barrier_details.csv',
            index=False,
            float_format=lambda n: round(n,2)
        )