Ignore:
Timestamp:
Sep 26, 2017 3:18:01 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
9be22cd, fca1f50
Parents:
24d9e84
Message:

fix image handling for saved reports

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/fitting/report_dialog.py

    r78312f7 r69a6897  
    5757 
    5858        fName = dlg.GetPath() 
     59        basename = os.path.splitext(fName)[0] 
    5960        ext_num = dlg.GetFilterIndex() 
     61        dlg.Destroy() 
     62 
     63        if ext_num == 0 and self.index_offset == 0:  # has pdf 
     64            ext = ".pdf" 
     65        elif ext_num == 1 - self.index_offset: 
     66            ext = ".html" 
     67        elif ext_num == 2 - self.index_offset: 
     68            ext = ".txt" 
     69        else: 
     70            logger.warn("unknown export format in report dialog") 
     71            return 
     72        filename = basename + ext 
     73 
     74        # save figures 
     75        pictures = [] 
     76        for num in range(self.nimages): 
     77            pic_name = basename + '_img%s.png' % num 
     78            # save the image for use with pdf writer 
     79            self.report_list[2][num].savefig(pic_name) 
     80            pictures.append(pic_name) 
     81 
     82        # translate png references int html from in-memory name to on-disk name 
     83        html = self.report_html.replace("memory:img_fit", basename+'_img') 
    6084 
    6185        #set file extensions 
    6286        img_ext = [] 
    63         pic_fname = [] 
    64         #PDF 
    65         if ext_num == (0 + 2 * self.index_offset): 
    66             # TODO: Sort this case out 
    67             ext = '.pdf' 
     87        if ext == ".pdf": 
     88            # write pdf as a pdf file 
     89            pdf = self.HTML2PDF(data=html, filename=filename) 
    6890 
    69             fName = os.path.splitext(fName)[0] + ext 
    70             dlg.Destroy() 
    71             #pic (png) file path/name 
    72             for num in range(self.nimages): 
    73                 im_ext = '_img%s.png' % num 
    74                 #img_ext.append(im_ext) 
    75                 pic_name = os.path.splitext(fName)[0] + im_ext 
    76                 pic_fname.append(pic_name) 
    77                 # save the image for use with pdf writer 
    78                 self.report_list[2][num].savefig(pic_name) 
     91            # delete images used to create the pdf 
     92            for pic_name in pictures: 
     93                os.remove(pic_name) 
    7994 
    80             #put the image path in html string 
    81             report_frame = self.report_list[0] 
    82             #put image name strings into the html file 
    83             #Note:The str for pic_fname shouldn't be removed. 
    84             if self.nimages == 1: 
    85                 html = report_frame % str(pic_fname[0]) 
    86             elif self.nimages == 2: 
    87                 html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) 
    88             elif self.nimages == 3: 
    89                 html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), 
    90                                        str(pic_fname[2])) 
    91  
    92             # make/open file in case of absence 
    93             f = open(fName, 'w') 
    94             f.close() 
    95             # write pdf as a pdf file 
    96             pdf = self.HTML2PDF(data=html, filename=fName) 
    97  
    98             #open pdf 
     95            #open pdf viewer 
    9996            if pdf: 
    10097                try: 
    101                     #Windows 
    102                     os.startfile(str(fName)) 
    103                 except Exception: 
    104                     try: 
    105                         #Mac 
     98                    if os.name == 'nt':  # Windows 
     99                        os.startfile(fName) 
     100                    elif sys.platform == "darwin":  # Mac 
    106101                        os.system("open %s" % fName) 
    107                     except Exception: 
    108                         #DO not open 
    109                         pass 
    110             #delete image file 
    111             for num in range(self.nimages): 
    112                 os.remove(pic_fname[num]) 
    113             return 
    114         #HTML + png(graph) 
    115         elif ext_num == (1 - self.index_offset): 
    116             ext = '.html' 
    117             for num in range(self.nimages): 
    118                 img_ext.append('_img4html%s.png' % num) 
    119             report_frame = self.report_list[0] 
    120         #TEXT + pdf(graph) 
    121         elif ext_num == (2 - self.index_offset): 
    122             ext = '.txt' 
    123             # changing the image extension actually changes the image 
    124             # format on saving 
    125             for num in range(self.nimages): 
    126                 img_ext.append('_img4txt%s.pdf' % num) 
    127             report = self.report_list[1] 
    128         else: 
    129             return 
     102                except Exception as exc: 
     103                    # cannot open pdf 
     104                    logging.error(str(exc)) 
    130105 
    131         #file name 
    132         fName = os.path.splitext(fName)[0] + ext 
    133         dlg.Destroy() 
     106        elif ext == ".html": 
     107            with open(filename, 'w') as f: 
     108                f.write(html) 
    134109 
    135         #pic (png) file path/name 
    136         for num in range(self.nimages): 
    137             pic_name = os.path.splitext(fName)[0] + img_ext[num] 
    138             pic_fname.append(pic_name) 
    139         #put the image path in html string 
    140         if ext_num == (1 - self.index_offset): 
    141             if self.nimages == 1: 
    142                 report = report_frame % os.path.basename(pic_fname[0]) 
    143             elif self.nimages == 2: 
    144                 report = report_frame % (os.path.basename(pic_fname[0]), 
    145                                          os.path.basename(pic_fname[1])) 
    146             elif self.nimages == 3: 
    147                 report = report_frame % (os.path.basename(pic_fname[0]), 
    148                                          os.path.basename(pic_fname[1]), 
    149                                          os.path.basename(pic_fname[2])) 
    150         f = open(fName, 'w') 
    151         f.write(report) 
    152         f.close() 
     110        elif ext == ".txt": 
     111            with open(filename, 'w') as f: 
     112                f.write(self.report_list[1]) 
     113 
    153114        self.Update() 
    154         #save png file using pic_fname 
    155         for num in range(self.nimages): 
    156             self.report_list[2][num].savefig(pic_fname[num]) 
Note: See TracChangeset for help on using the changeset viewer.