Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import dotenv
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
|
| 6 |
+
from finter.data import ModelData
|
| 7 |
+
from finter.framework_model import BaseAlpha
|
| 8 |
+
|
| 9 |
+
# .env 파일에서 환경 변수 로드
|
| 10 |
+
dotenv.load_dotenv(dotenv.find_dotenv())
|
| 11 |
+
|
| 12 |
+
END = 20240401
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def plot_sector_area_chart(model_name):
|
| 16 |
+
df = ModelData.load(model_name)
|
| 17 |
+
df.columns = df.columns.astype(int)
|
| 18 |
+
|
| 19 |
+
sector = BaseAlpha.get_cm(
|
| 20 |
+
"content.handa.dataguide.company.krx-spot-sector.1d"
|
| 21 |
+
).get_df(20190101, END)
|
| 22 |
+
sector_list = set(sector.unstack().dropna())
|
| 23 |
+
|
| 24 |
+
def get_sector(sector_name):
|
| 25 |
+
return (df[sector == sector_name]).sum(axis=1).rename(sector_name)
|
| 26 |
+
|
| 27 |
+
weight = pd.concat([get_sector(sector_name) for sector_name in sector_list], axis=1)
|
| 28 |
+
weight = pd.concat(
|
| 29 |
+
[
|
| 30 |
+
weight,
|
| 31 |
+
df[sector.reindex(columns=df.columns).isna()].sum(axis=1).rename("Etc"),
|
| 32 |
+
],
|
| 33 |
+
axis=1,
|
| 34 |
+
)
|
| 35 |
+
df_long = weight.reset_index().melt(
|
| 36 |
+
id_vars="index", var_name="Category", value_name="Value"
|
| 37 |
+
)
|
| 38 |
+
df_long.rename(columns={"index": "Date"}, inplace=True)
|
| 39 |
+
|
| 40 |
+
fig = px.area(
|
| 41 |
+
df_long,
|
| 42 |
+
x="Date",
|
| 43 |
+
y="Value",
|
| 44 |
+
color="Category",
|
| 45 |
+
title="Time Series Area Chart for Industry Sectors",
|
| 46 |
+
)
|
| 47 |
+
return fig
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
with gr.Blocks() as app:
|
| 52 |
+
gr.Markdown(
|
| 53 |
+
"**Explore the Source Code: [Click Here!](https://quantit.gitbook.io/finter/v/cook-book)**"
|
| 54 |
+
)
|
| 55 |
+
with gr.Row():
|
| 56 |
+
model_input = gr.Textbox(
|
| 57 |
+
value="alpha.krx.krx.stock.ldh0127.div_2", label="Enter model name"
|
| 58 |
+
)
|
| 59 |
+
submit_button = gr.Button("Plot Chart")
|
| 60 |
+
chart_output = gr.Plot()
|
| 61 |
+
|
| 62 |
+
submit_button.click(
|
| 63 |
+
fn=plot_sector_area_chart, inputs=model_input, outputs=chart_output
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|