# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

"|IMPORT PACKAGES|"
import pandas             as pd
from   bokeh.plotting     import show, figure, output_file, save
from   bokeh.io           import show, output_notebook, curdoc, export_png
from   bokeh.models       import ColumnDataSource,LinearAxis, Range1d, NumeralTickFormatter, LabelSet, Label, BoxAnnotation
from   bokeh.models.tools import HoverTool

# +
"|IMPORT DATA|"
path = r'https://github.com/ncachanosky/research/blob/master/Economic%20Series/'
file = r'Resumen%20Estadistico%20-%20Argentina.xlsx?raw=true'
IO   = path + file

sheet = 'DEFICIT FINANCIERO'

data = pd.read_excel(IO, sheet_name = sheet, usecols="A:E", skiprows=0, nrows=60, engine='openpyxl') # Be patient...

data = data.rename(columns={"Fiscal deficit [Federal level]":"DEFICIT_1"})        # Nación
data = data.rename(columns={"Fiscal deficit [Nation and Provinces]":"DEFICIT_2"}) # Consolidado
data = data.rename(columns={"Resultado primario":"PRIMARIO"})                     # Consolidado

data["DEFICIT_1"] = data["DEFICIT_1"]*100
data["DEFICIT_2"] = data["DEFICIT_2"]*100
data["PRIMARIO"]  = data["PRIMARIO"] *100
# -

"|CHECK DATA|"
data

# +
"|BUILD PLOT|"
cds = ColumnDataSource(data)

CRISES_X = [data.iloc[14,0], # 1
            data.iloc[21,0], # 2
            data.iloc[27,0], # 3
            data.iloc[40,0], # 4
            data.iloc[53,0], # 5
            data.iloc[58,0]] # 6

CRISES_Y = [data.iloc[14,4] - 0.5, # 1 
            data.iloc[21,4] - 0.5, # 2
            data.iloc[27,4] - 0.5, # 3
            data.iloc[40,4] - 0.5, # 4
            data.iloc[53,4] - 0.5, # 5
            data.iloc[58,4] - 0.5] # 6

colors = ["green" , # 1
          "maroon", # 2
          "maroon", # 3
          "maroon", # 4
          "maroon", # 5
          "maroon"] # 6

#Default and Rodrigazo notes
note1 = Label(x=data.iloc[14,0]+1.0, y=CRISES_Y[0]  , text="Rodrigazo"      , text_font_size="10pt", text_align='left' , text_baseline='middle', text_color="green")
note2 = Label(x=data.iloc[21,0]+1.0, y=CRISES_Y[1]  , text="Default"        , text_font_size="10pt", text_align='left' , text_baseline='middle', text_color="maroon")
note3 = Label(x=data.iloc[27,0]+1.0, y=CRISES_Y[2]  , text="Default"        , text_font_size="10pt", text_align='left' , text_baseline='middle', text_color="maroon")
note4 = Label(x=data.iloc[40,0]+1.0, y=CRISES_Y[3]  , text="Default"        , text_font_size="10pt", text_align='left' , text_baseline='middle', text_color="maroon")
note5 = Label(x=data.iloc[53,0]-1.0, y=CRISES_Y[4]  , text="Default"        , text_font_size="10pt", text_align='right', text_baseline='middle', text_color="maroon")
note6 = Label(x=data.iloc[58,0]    , y=CRISES_Y[5]+1, text="Reperfilamiento", text_font_size="10pt", text_align='right', text_baseline='middle', text_color="maroon")

#Presidents' shades and names
Frondizi     = BoxAnnotation(left=1961, right=1962, bottom=0, line_color="black", fill_color="red"    , fill_alpha=0.1)
Guido        = BoxAnnotation(left=1962, right=1963, bottom=0, line_color="black", fill_color="red"    , fill_alpha=0.1)
Illia        = BoxAnnotation(left=1963, right=1966, bottom=0, line_color="black", fill_color="red"    , fill_alpha=0.1)
Ongania      = BoxAnnotation(left=1966, right=1970, bottom=0, line_color="black", fill_color="green"  , fill_alpha=0.1)
Junta1       = BoxAnnotation(left=1970, right=1973, bottom=0, line_color="black", fill_color="green"  , fill_alpha=0.1)
Peron        = BoxAnnotation(left=1973, right=1976, bottom=0, line_color="black", fill_color="blue"   , fill_alpha=0.1)
Junta2       = BoxAnnotation(left=1976, right=1983, bottom=0, line_color="black", fill_color="green"  , fill_alpha=0.1)
Alfonsin     = BoxAnnotation(left=1983, right=1989, bottom=0, line_color="black", fill_color="red"    , fill_alpha=0.1)
Menem        = BoxAnnotation(left=1989, right=1999, bottom=0, line_color="black", fill_color="blue"   , fill_alpha=0.1)
DeLaRua      = BoxAnnotation(left=1999, right=2001, bottom=0, line_color="black", fill_color="red"    , fill_alpha=0.1)
Duhalde      = BoxAnnotation(left=2001, right=2003, bottom=0, line_color="black", fill_color="blue"   , fill_alpha=0.1)
NKirchner    = BoxAnnotation(left=2003, right=2007, bottom=0, line_color="black" ,fill_color="blue"   , fill_alpha=0.1)
CFK          = BoxAnnotation(left=2007, right=2015, bottom=0, line_color="black" ,fill_color="blue"   , fill_alpha=0.1)
Macri        = BoxAnnotation(left=2015, right=2019, bottom=0, line_color="black", fill_color="yellow" , fill_alpha=0.1)
Fernandez    = BoxAnnotation(left=2019, right=2020, bottom=0, line_color="black", fill_color="blue"   , fill_alpha=0.1)

Frondizi_N   = Label(x=1962  , y=9.5, text="Frondizi"     , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Guido_N      = Label(x=1963  , y=9.5, text="<NAME>"  , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Illia_N      = Label(x=1964.5, y=9.5, text="Illia"        , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Ongania_N    = Label(x=1967.5, y=9.5, text="Onganía"      , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Junta1_N     = Label(x=1971.5, y=9.5, text="<NAME>", text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Peron_N      = Label(x=1974.5, y=9.5, text="Perón-Perón"  , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Junta2_N     = Label(x=1977.5, y=9.5, text="<NAME>", text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Alfonsin_N   = Label(x=1984.5, y=9.5, text="Alfonsín"     , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Menem_N      = Label(x=1990.5, y=9.5, text="Menem"        , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
DeLaRua_N    = Label(x=2000.5, y=9.5, text="<NAME>"    , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Duhalde_N    = Label(x=2002.5, y=9.5, text="Duhalde"      , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
NKirchner_N  = Label(x=2004.5, y=9.5, text="<NAME>"  , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
CFK_N        = Label(x=2008.5, y=9.5, text="<NAME>"  , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Macri_N      = Label(x=2016.5, y=9.5, text="Macri"        , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)
Fernandez_N  = Label(x=2021.5, y=9.5, text="Fernández"    , text_font_size="10pt", text_align="right", angle_units="deg", angle=90)


#BUILD FIGURE
p = figure(title        = "EL HUB ECONÓMICO | DEFICIT FISCAL (%DEL PBI)",
           x_range      = (1961, 2021),
           y_range      = (-16, 10),
           y_axis_label = "% PBI",
           plot_height  = 400,
           plot_width   = 700)

p.toolbar_location = "right"
p.toolbar.autohide = True

#AXIS 1
p.line("YEAR" , "DEFICIT_1", legend_label="Nación"     , line_color="black", alpha=1.00, muted_alpha=0.2, line_width=2, source=cds)
p.varea("YEAR", y1=0, y2="DEFICIT_2", legend_label="Consolidado", fill_color="red"  , alpha=0.50, muted_alpha=0.2, source=cds)
p.circle(CRISES_X, CRISES_Y, size=15, color=colors     , fill_alpha=0.50   , muted_alpha=0.2)

p1 = p.line("YEAR", "DEFICIT_2", line_color="red", source=cds)

p.add_tools(HoverTool(renderers=[p1], tooltips = [("Año", "@YEAR"), ("Nación", "@DEFICIT_1{0.0}"), ("Consolidado", "@DEFICIT_2{0.0}")], mode='vline'))


#ADD CRISES NOTES
p.add_layout(note1)
p.add_layout(note2)
p.add_layout(note3)
p.add_layout(note4)
p.add_layout(note5)
p.add_layout(note6)

#ADD PRSEIDENTS' SHADES
p.add_layout(Frondizi)
p.add_layout(Guido)
p.add_layout(Illia)
p.add_layout(Ongania)
p.add_layout(Junta1)
p.add_layout(Peron)
p.add_layout(Junta2)
p.add_layout(Alfonsin)
p.add_layout(Menem)
p.add_layout(DeLaRua)
p.add_layout(Duhalde)
p.add_layout(NKirchner)
p.add_layout(CFK)
p.add_layout(Macri)
p.add_layout(Fernandez)

#ADD PRSIDENTS' NAMES
p.add_layout(Frondizi_N)
p.add_layout(Guido_N)
p.add_layout(Illia_N)
p.add_layout(Ongania_N)
p.add_layout(Junta1_N)
p.add_layout(Peron_N)
p.add_layout(Junta2_N)
p.add_layout(Alfonsin_N)
p.add_layout(Menem_N)
p.add_layout(DeLaRua_N)
p.add_layout(Duhalde_N)
p.add_layout(NKirchner_N)
p.add_layout(CFK_N)
p.add_layout(Macri_N)
#p.add_layout(Fernandez_N)

#LEGEND
p.legend.location     = "bottom_right"
p.legend.orientation  = "horizontal"
p.legend.click_policy = "mute"
show(p)

# +
"|EXPORT .PNG FILE|"

export_png(p, filename="fiscal_deficit.png")

# +
"|# CREATE HTML FILE|"

output_file(filename="fiscal_deficit.html", title="fiscal_deficit")
save(p)

# +
"|CREATE JSON FILE|"
import json
import bokeh.embed

from bokeh.embed import json_item

j = json.dumps(json_item(p, "fiscal_deficit"))
with open("fiscal_deficit.json", "w") as fp:
    json.dump(j, fp)
# -



StarcoderdataJupyterScriptsDedupFiltered