Fixed missing image issue (absolute path fix)
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import gradio as gr
|
| 4 |
-
import matplotlib.colors as mcolors
|
|
|
|
| 5 |
|
| 6 |
def hex_to_rgb(hex_color):
|
| 7 |
"""Convert HEX color (e.g., '#ff5733') to an RGB tuple for Matplotlib."""
|
|
@@ -21,7 +22,7 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
|
| 21 |
y_values = func(x_values)
|
| 22 |
|
| 23 |
# Convert HEX color to RGB before passing to Matplotlib
|
| 24 |
-
rgb_color = hex_to_rgb(color)
|
| 25 |
|
| 26 |
plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
|
| 27 |
|
|
@@ -29,15 +30,17 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
|
| 29 |
plt.ylabel("f(x)")
|
| 30 |
plt.title("Function Plot")
|
| 31 |
plt.legend()
|
| 32 |
-
|
| 33 |
if grid:
|
| 34 |
plt.grid()
|
| 35 |
|
|
|
|
| 36 |
plot_filename = "high_res_plot.png"
|
| 37 |
-
|
|
|
|
| 38 |
plt.close()
|
| 39 |
|
| 40 |
-
return
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error: {e}", None
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import gradio as gr
|
| 4 |
+
import matplotlib.colors as mcolors
|
| 5 |
+
import os # Import os to handle file paths
|
| 6 |
|
| 7 |
def hex_to_rgb(hex_color):
|
| 8 |
"""Convert HEX color (e.g., '#ff5733') to an RGB tuple for Matplotlib."""
|
|
|
|
| 22 |
y_values = func(x_values)
|
| 23 |
|
| 24 |
# Convert HEX color to RGB before passing to Matplotlib
|
| 25 |
+
rgb_color = hex_to_rgb(color)
|
| 26 |
|
| 27 |
plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
|
| 28 |
|
|
|
|
| 30 |
plt.ylabel("f(x)")
|
| 31 |
plt.title("Function Plot")
|
| 32 |
plt.legend()
|
| 33 |
+
|
| 34 |
if grid:
|
| 35 |
plt.grid()
|
| 36 |
|
| 37 |
+
# Save the plot as an absolute path
|
| 38 |
plot_filename = "high_res_plot.png"
|
| 39 |
+
abs_path = os.path.abspath(plot_filename) # Convert to absolute path
|
| 40 |
+
plt.savefig(abs_path, dpi=300)
|
| 41 |
plt.close()
|
| 42 |
|
| 43 |
+
return abs_path, abs_path # Return the absolute path for Gradio to use
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error: {e}", None
|