|
import pandas as pd |
|
import numpy as np |
|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
import japanize_matplotlib |
|
import matplotlib.dates as mdates |
|
from dateutil.relativedelta import relativedelta |
|
import datetime |
|
from func import get_fish_qty, get_estat, dr_prediction_deployment, prediction_func |
|
|
|
import yaml |
|
with open('config.yaml') as file: |
|
config = yaml.safe_load(file.read()) |
|
|
|
def get_prediction_result(): |
|
today = datetime.datetime.now() |
|
prediction_month = (today + relativedelta(months=1)).strftime('%Y%m') |
|
df = prediction_func.prediction_to_dr(config['oil_price_url'], config['fuel_procurement_cost_url']) |
|
df = df.loc[df['target_date'].astype(str).str[:6]==prediction_month] |
|
df['target_date'] = pd.to_datetime(df['target_date'].astype(str)) |
|
return df[['target_date', '電気代']] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plot_prediction_result(): |
|
df = get_prediction_result() |
|
|
|
fig, ax1 = plt.subplots(1, 1, figsize=(20, 5)) |
|
fig.autofmt_xdate() |
|
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d")) |
|
ax1.xaxis.set_major_locator(mdates.DayLocator(interval=1)) |
|
plt.rcParams["font.size"] = 14 |
|
ax1.grid(True) |
|
ax1.set_xlim(df['target_date'].min(), df['target_date'].max()) |
|
ax1.set_xlabel('target_date') |
|
ax1.set_ylabel('電気代(円)') |
|
ax1.plot(df['target_date'], df['電気代']) |
|
ax1.legend(loc="upper left", bbox_to_anchor=(1.0, 1.0)) |
|
|
|
return fig |
|
|
|
|
|
with gr.Blocks() as electoric_ploting: |
|
gr.Markdown("# 📈 Real-Time Line Plot") |
|
with gr.Row(): |
|
|
|
plot = gr.Plot(label="Plot") |
|
with gr.Row(): |
|
gr.DataFrame(get_prediction_result) |
|
|
|
|
|
|
|
|
|
|
|
electoric_ploting.load(lambda: datetime.datetime.now(), |
|
None, |
|
|
|
every=360) |
|
dep = electoric_ploting.load(plot_prediction_result, None, plot, every=360) |
|
|
|
electoric_ploting.queue().launch() |
|
|
|
plt.close() |