Skip to content

Commit b417d61

Browse files
authored
Update local.py
1 parent 2f6e5f7 commit b417d61

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

pystackql/magic_ext/local.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,48 @@ def stackql(self, line, cell=None):
6262
elif not is_cell_magic:
6363
return results
6464

65-
def _display_with_csv_download(self, df):
66-
"""Display DataFrame with CSV download link.
65+
def _display_with_csv_download(self, df):
66+
"""Display DataFrame with CSV download link.
67+
68+
:param df: The DataFrame to display and make downloadable.
69+
"""
70+
import IPython.display
71+
72+
try:
73+
# Generate CSV data
74+
import io
75+
import base64
76+
csv_buffer = io.StringIO()
77+
df.to_csv(csv_buffer, index=False)
78+
csv_data = csv_buffer.getvalue()
79+
80+
# Encode to base64 for data URI
81+
csv_base64 = base64.b64encode(csv_data.encode()).decode()
82+
83+
# Create download link
84+
download_link = f'data:text/csv;base64,{csv_base64}'
85+
86+
# Display the DataFrame first
87+
IPython.display.display(df)
88+
89+
# Create and display the download button
90+
download_html = f'''
91+
<div style="margin-top: 10px;">
92+
<a href="{download_link}" download="stackql_results.csv"
93+
style="display: inline-block; padding: 8px 16px; background-color: #007cba;
94+
color: white; text-decoration: none; border-radius: 4px;
95+
font-family: Arial, sans-serif; font-size: 14px; border: none; cursor: pointer;">
96+
📥 Download CSV
97+
</a>
98+
</div>
99+
'''
100+
IPython.display.display(IPython.display.HTML(download_html))
101+
102+
except Exception as e:
103+
# If CSV generation fails, just display the DataFrame normally
104+
IPython.display.display(df)
105+
print(f"Error generating CSV download: {e}")
67106

68-
:param df: The DataFrame to display and make downloadable.
69-
"""
70-
try:
71-
# Generate CSV data
72-
csv_buffer = io.StringIO()
73-
df.to_csv(csv_buffer, index=False)
74-
csv_data = csv_buffer.getvalue()
75-
76-
# Encode to base64 for data URI
77-
csv_base64 = base64.b64encode(csv_data.encode()).decode()
78-
79-
# Create download link
80-
download_link = f'data:text/csv;base64,{csv_base64}'
81-
82-
# Display the DataFrame first
83-
display(df)
84-
85-
# Create and display the download button
86-
download_html = f'''
87-
<div style="margin-top: 10px;">
88-
<a href="{download_link}" download="stackql_results.csv"
89-
style="display: inline-block; padding: 8px 16px; background-color: #007cba;
90-
color: white; text-decoration: none; border-radius: 4px;
91-
font-family: Arial, sans-serif; font-size: 14px; border: none; cursor: pointer;">
92-
📥 Download CSV
93-
</a>
94-
</div>
95-
'''
96-
display(HTML(download_html))
97-
98-
except Exception as e:
99-
# If CSV generation fails, just display the DataFrame normally
100-
display(df)
101-
print(f"Error generating CSV download: {e}")
102-
103107
def load_ipython_extension(ipython):
104108
"""Load the non-server magic in IPython.
105109
@@ -110,4 +114,4 @@ def load_ipython_extension(ipython):
110114
"""
111115
# Create an instance of the magic class and register it
112116
magic_instance = StackqlMagic(ipython)
113-
ipython.register_magics(magic_instance)
117+
ipython.register_magics(magic_instance)

0 commit comments

Comments
 (0)