11import os
2- import time
2+ import tempfile
33from typing import Any
44
55from IPython .core .interactiveshell import InteractiveShell
@@ -32,6 +32,7 @@ def run_personal(self, line: str, local_ns: Any = None) -> Any:
3232 %run_personal {{ sample_notebook_name }}
3333
3434 """
35+
3536 template = Template (line .strip ())
3637 personal_file = template .render (local_ns )
3738 if not personal_file :
@@ -42,16 +43,14 @@ def run_personal(self, line: str, local_ns: Any = None) -> Any:
4243 if not personal_file :
4344 raise ValueError ('No personal file specified.' )
4445
45- local_filename = (
46- f'{ int (time .time () * 1_000_000 )} _{ personal_file } ' .replace (' ' , '_' )
47- )
48- sql_command = f"DOWNLOAD PERSONAL FILE '{ personal_file } ' TO '{ local_filename } '"
49-
50- # Execute the SQL command
51- self .shell .run_line_magic ('sql' , sql_command )
52- # Run the downloaded file
53- self .shell .run_line_magic ('run' , local_filename )
54-
55- # Delete the local file after running it
56- if os .path .exists (local_filename ):
57- os .remove (local_filename )
46+ with tempfile .TemporaryDirectory () as temp_dir :
47+ temp_file_path = os .path .join (temp_dir , personal_file )
48+ sql_command = (
49+ f"DOWNLOAD PERSONAL FILE '{ personal_file } ' "
50+ f"TO '{ temp_file_path } '"
51+ )
52+
53+ # Execute the SQL command
54+ self .shell .run_line_magic ('sql' , sql_command )
55+ # Run the downloaded file
56+ self .shell .run_line_magic ('run' , f'"{ temp_file_path } "' )
0 commit comments