Spaces:
Sleeping
Sleeping
Update pyfolio/mcp_output/mcp_plugin/mcp_service.py
Browse files
pyfolio/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -37,34 +37,39 @@ from pyfolio.plotting import (
|
|
| 37 |
|
| 38 |
mcp = FastMCP("pyfolio_service")
|
| 39 |
|
| 40 |
-
def _save_plot_as_base64(
|
| 41 |
-
"""Save
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def _convert_to_series(data: List[float], name: str = "returns") -> pd.Series:
|
| 70 |
"""Convert list to pandas Series with date index."""
|
|
|
|
| 37 |
|
| 38 |
mcp = FastMCP("pyfolio_service")
|
| 39 |
|
| 40 |
+
def _save_plot_as_base64(plt, plot_name):
|
| 41 |
+
"""Save plot as base64 string and to file"""
|
| 42 |
+
import base64
|
| 43 |
+
from io import BytesIO
|
| 44 |
+
import datetime
|
| 45 |
+
import os
|
| 46 |
+
|
| 47 |
+
# Generate timestamp for unique filename
|
| 48 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 49 |
+
filename = f"{plot_name}_{timestamp}.png"
|
| 50 |
+
|
| 51 |
+
# Save to current working directory instead of /tmp
|
| 52 |
+
current_dir = os.getcwd()
|
| 53 |
+
filepath = os.path.join(current_dir, filename)
|
| 54 |
+
|
| 55 |
+
# Save to file
|
| 56 |
+
plt.savefig(filepath, dpi=300, bbox_inches='tight')
|
| 57 |
+
|
| 58 |
+
# Convert to base64
|
| 59 |
+
buffer = BytesIO()
|
| 60 |
+
plt.savefig(buffer, format='png', dpi=300, bbox_inches='tight')
|
| 61 |
+
buffer.seek(0)
|
| 62 |
+
image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 63 |
+
buffer.close()
|
| 64 |
+
|
| 65 |
+
# Create data URI
|
| 66 |
+
data_uri = f"data:image/png;base64,{image_base64}"
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"image_base64": image_base64,
|
| 70 |
+
"image_path": filepath,
|
| 71 |
+
"data_uri": data_uri
|
| 72 |
+
}
|
| 73 |
|
| 74 |
def _convert_to_series(data: List[float], name: str = "returns") -> pd.Series:
|
| 75 |
"""Convert list to pandas Series with date index."""
|