|
import ssl |
|
import logging |
|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
from io import BytesIO |
|
import base64 |
|
from datetime import datetime |
|
|
|
|
|
|
|
|
|
|
|
ssl._create_default_https_context = ssl._create_unverified_context |
|
|
|
current_time = datetime.now().strftime("%b-%d-%Y") |
|
|
|
def load_css(): |
|
with open('style.css', 'r', encoding='utf-8') as file: |
|
css = file.read() |
|
return f"<style>{css}</style>" |
|
|
|
|
|
def format_quantity(quantity): |
|
|
|
if abs(quantity) < 1e-5: |
|
quantity = 0 |
|
if quantity < 0: |
|
return f"({-quantity:,.1f})" |
|
else: |
|
return f"{quantity:,.1f}" |
|
|
|
def format_value(value): |
|
|
|
if abs(value) < 1e-5: |
|
value = 0 |
|
if value < 0: |
|
return f"({-value:,.0f})" |
|
else: |
|
return f"{value:,.0f}" |
|
|
|
currency_symbols = { |
|
"KRW": "โฉ", |
|
"USD": "$", |
|
"CAD": "$", |
|
"EUR": "โฌ", |
|
"JPY": "ยฅ", |
|
"GBP": "ยฃ" |
|
} |
|
|
|
def get_currency_symbol(currency_code): |
|
return currency_symbols.get(currency_code.upper(), "") |
|
|
|
def get_currency_codes(): |
|
return list(currency_symbols.keys()) |
|
|
|
currency_codes = get_currency_codes() |
|
|
|
|
|
def clear_buttons(inputs): |
|
clear_button = gr.ClearButton(value="Clear") |
|
clear_button.click( |
|
fn=lambda: [None] * len(inputs), |
|
inputs=[], |
|
outputs=inputs |
|
) |
|
return clear_button |
|
|
|
def submit_buttons(inputs, update_fn, output): |
|
submit_button = gr.Button(value="Run", variant="primary") |
|
submit_button.click( |
|
fn=update_fn, |
|
inputs=inputs, |
|
outputs=output |
|
) |
|
return submit_button |
|
|
|
def on_change(inputs, update_ouutput, outputs): |
|
for input_component in inputs: |
|
input_component.change( |
|
fn=update_ouutput, |
|
inputs=inputs, |
|
outputs=outputs |
|
) |
|
|
|
def render_components(component_rows): |
|
for row in component_rows: |
|
if isinstance(row, list): |
|
with gr.Row(): |
|
for component in row: |
|
component.render() |
|
else: |
|
row.render() |
|
|
|
def create_tab(tab_name, inputs, outputs, update_fn, examples, component_rows): |
|
with gr.TabItem(tab_name): |
|
with gr.Row(): |
|
with gr.Column(elem_classes="input"): |
|
render_components(component_rows) |
|
clear_buttons(inputs) |
|
submit_buttons(inputs, update_fn, outputs) |
|
gr.Examples(examples=examples, cache_examples=False, inputs=inputs) |
|
with gr.Column(): |
|
outputs.render() |
|
on_change(inputs, update_fn, outputs) |
|
|
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
from io import BytesIO |
|
import base64 |
|
from matplotlib import font_manager |
|
|
|
|
|
color_map_storage = {} |
|
|
|
def get_color_for_label(index, color_map, num_labels): |
|
"""Retrieve or generate color for the given index.""" |
|
if index not in color_map_storage: |
|
cmap = plt.get_cmap(color_map) |
|
|
|
color_map_storage[index] = cmap(1 - index / (num_labels - 1)) |
|
return color_map_storage[index] |
|
|
|
def plot_donut_chart(data, color_map='Blues', font_path='Quicksand-Regular.ttf', legend_fontsize=30): |
|
|
|
filtered_data = {k: v for k, v in data.items() if v > 0} |
|
|
|
if not filtered_data: |
|
return '<p>No data to display.</p>' |
|
|
|
|
|
sorted_data = sorted(filtered_data.items(), key=lambda item: item[1], reverse=True) |
|
labels, sizes = zip(*sorted_data) |
|
|
|
|
|
num_labels = len(labels) |
|
|
|
|
|
colors = [get_color_for_label(i, color_map, num_labels) for i in range(num_labels)] |
|
|
|
|
|
fig, ax = plt.subplots(figsize=(12, 8), dpi=300) |
|
wedges, _ = ax.pie( |
|
sizes, |
|
colors=colors, |
|
labels=[None]*num_labels, |
|
autopct=None, |
|
startangle=-90, |
|
pctdistance=0.85, |
|
wedgeprops=dict(width=0.4, edgecolor='w') |
|
) |
|
|
|
|
|
ax.invert_yaxis() |
|
|
|
|
|
handles = [plt.Line2D([0], [0], marker='o', color='w', label=f'{label} {size * 100:.1f}%', |
|
markersize=15, markerfacecolor=get_color_for_label(i, color_map, num_labels)) |
|
for i, (label, size) in enumerate(zip(labels, sizes))] |
|
|
|
|
|
ax.legend(handles=handles, loc='upper left', bbox_to_anchor=(1, 1), |
|
prop=font_manager.FontProperties(fname=font_path, size=legend_fontsize), frameon=False) |
|
|
|
|
|
ax.axis('off') |
|
|
|
|
|
buf = BytesIO() |
|
plt.savefig(buf, format='svg', bbox_inches='tight') |
|
plt.close(fig) |
|
buf.seek(0) |
|
|
|
|
|
svg_str = buf.getvalue().decode('utf-8') |
|
return f'<img src="data:image/svg+xml;base64,{base64.b64encode(svg_str.encode("utf-8")).decode("utf-8")}" />' |
|
|