1111from most_used_by_type_bar import most_used_by_type_bar
1212from toggle_bars import toggle_bars
1313
14- import os
14+ import tempfile , os , shutil
1515
1616app = Flask (__name__ )
1717
@@ -59,9 +59,6 @@ def Sankey_Run():
5959 #instance_ur = 'https://synbiohub.org/'
6060 #url = 'https://synbiohub.org/public/igem/BBa_B0012/1'
6161 #top_level_url = 'https://dev.synbiohub.org/public/igem/BBa_B0012/1'
62-
63- #get current working directory
64- cwd = os .getcwd ()
6562
6663 #retrieve information about the poi
6764 self_df , display_id , title , role , count = input_data (top_level_url , instance_url )
@@ -74,18 +71,22 @@ def Sankey_Run():
7471 df_sankey = sankey (url , top_level_url , title , instance_url )
7572
7673 sankey_title = "Parts Co-Located with " + title + " (a " + role_link + ")"
77- filename = os .path .join (cwd , f'sankey_{ display_id } _.html' )
74+
75+ #create a temporary directory
76+ temp_dir = tempfile .TemporaryDirectory ()
77+
78+ #name file
79+ filename = os .path .join (temp_dir .name , "Sankey.html" )
7880
81+
7982 #create the sankey diagram
8083 sankey_graph (filename , df_sankey , 'Node, Label' ,
8184 'Link' , 'Color' , 'Source' ,'Target' , 'Value' ,
8285 'Link Color' , sankey_title , url_not_name = False )
86+
8387
8488 #obtain the html from the sankey diagram
8589 result = retrieve_html (filename )
86-
87- #delete the copy of the sankey diagram on the server
88- os .remove (filename )
8990
9091 return result
9192 except Exception as e :
@@ -133,9 +134,6 @@ def Bar_Run():
133134 url = complete_sbol .replace ('/sbol' ,'' )
134135
135136 try :
136-
137- #current working directory
138- cwd = os .getcwd ()
139137
140138 #create input data
141139 self_df , display_id , title , role , count = input_data (top_level_url , instance_url )
@@ -147,39 +145,36 @@ def Bar_Run():
147145 #graph title for most used barchart
148146 graph_title = f'Top Ten Parts by Number of Uses Compared to <a href="{ url } " target="_blank">{ title } </a>'
149147
150- #where to save the file
151- filename1 = os .path .join (cwd , f'bar1_{ display_id } .html' )
148+ #create a temporary directory
149+ temp_dir = tempfile .TemporaryDirectory ()
150+
151+ #name file
152+ filename1 = os .path .join (temp_dir .name , "Most_Used.html" )
152153
153154 #create the most used barchart
154155 bar_plot ('title' ,'count' ,'color' ,bar_df , graph_title , filename1 , 'deff' )
155156
156157 #retrieve html
157158 most_used = retrieve_html (filename1 )
158159
159- #remove file
160- os .remove (filename1 )
161-
162160 #find poi role ontology link
163161 role_link = find_role_name (role , plural = False )
164162
165163 bar_df = most_used_by_type_bar (top_level_url ,instance_url , display_id , title ,
166164 role , count )
167-
165+
168166 #graph title for most used barchart
169167 graph_title = f'Top Ten { role_link } by Number of Uses Compared to <a href="{ url } " target="_blank">{ title } </a>'
170168
171- #where to save the file
172- filename2 = os .path .join (cwd , f'bar2_ { display_id } .html' )
173-
169+ #name file
170+ filename2 = os .path .join (temp_dir . name , "Most_Used_Type .html" )
171+
174172 #create the most used barchart
175173 bar_plot ('title' ,'count' ,'color' ,bar_df , graph_title , filename2 , 'deff' )
176174
177175 #retrieve html
178176 by_role = retrieve_html (filename2 )
179177
180- #remove file
181- os .remove (filename2 )
182-
183178 #create bar toggle html
184179 toggle_display = toggle_bars (most_used ,by_role )
185180
0 commit comments