mirror of
https://github.com/Sosokker/Packaged-Food-Explorer.git
synced 2025-12-18 20:54:05 +01:00
Fix embed function + Add subplot pie/bar
This commit is contained in:
parent
aeb391eabf
commit
914c92ea4d
@ -1,4 +1,5 @@
|
||||
import os
|
||||
from plotly.subplots import make_subplots
|
||||
import plotly.graph_objects as go
|
||||
import plotly.io as pio
|
||||
import tempfile
|
||||
@ -35,13 +36,20 @@ class plotter:
|
||||
|
||||
if chart_type == 'bar':
|
||||
fig = go.Figure(data=go.Bar(x=nutrient_names, y=nutrient_values, marker_color='skyblue'))
|
||||
fig.update_layout(xaxis_title='Nutrients',
|
||||
fig.update_layout(title='Macronutrients Graph',xaxis_title='Nutrients',
|
||||
yaxis_title='Value')
|
||||
elif chart_type == 'pie':
|
||||
fig = go.Figure(data=go.Pie(labels=nutrient_names, values=nutrient_values))
|
||||
# fig.update_layout(title=f'Nutrient of {product_name}')
|
||||
fig.update_layout(title=f'Nutrient of {product_name}')
|
||||
elif chart_type == 'barpie':
|
||||
fig = make_subplots(rows=2, cols=2, specs=[[{"type": "xy"}, {"type": "domain"}], [{}, {}]])
|
||||
fig.add_trace(go.Pie(labels=nutrient_names, values=nutrient_values),
|
||||
row=1, col=2)
|
||||
fig.add_trace(go.Bar(x=nutrient_names, y=nutrient_values, marker_color='skyblue'),
|
||||
row=1, col=1)
|
||||
fig.update_layout(title=f'Nutrient of {product_name}')
|
||||
else:
|
||||
raise ValueError('Invalid chart type. Please choose either "bar" or "pie".')
|
||||
raise ValueError('Invalid chart type. Please choose either "bar" or "pie" or "barpie".')
|
||||
|
||||
html_string = pio.to_html(fig, include_plotlyjs='cdn', full_html=False)
|
||||
encoded_html = html_string.encode('utf-8')
|
||||
@ -49,11 +57,14 @@ class plotter:
|
||||
if popup:
|
||||
with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as temp:
|
||||
filename = temp.name
|
||||
print(filename)
|
||||
temp.write(encoded_html)
|
||||
webview.create_window('Nutrients Graph', filename)
|
||||
webview.start()
|
||||
temp.close()
|
||||
webview.create_window('Nutrients Graph', filename)
|
||||
webview.start()
|
||||
os.remove(filename)
|
||||
else:
|
||||
fig.update_layout(width=250, height=300)
|
||||
fig.update_layout(width=350, height=300)
|
||||
temp_dir = tempfile.mkdtemp()
|
||||
image_path = os.path.join(temp_dir, "plot.jpeg")
|
||||
fig.write_image(image_path, format="jpeg")
|
||||
@ -62,5 +73,6 @@ class plotter:
|
||||
label.img = ImageTk.PhotoImage(img)
|
||||
label['image'] = label.img
|
||||
label.pack()
|
||||
os.remove(image_path)
|
||||
|
||||
# print(type(nutrient_plotter(df, 1, [16, 18, 20, 22], 'bar', False, True)))
|
||||
Loading…
Reference in New Issue
Block a user