from json import load

from pandas import DataFrame, json_normalize

from constants import COLUMNS, FILES
from app.data_request import request_dataset


def open_json(dataset) -> dict:
    """
    Loads a dictionary with data from a JSON file
    """
    with open(FILES[dataset]) as f:
        json = load(f)
    return json


def create_dataframe(dataset) -> DataFrame:
    """
    Creates a DataFrame from a JSON file
    """
    request_dataset(dataset)
    json = open_json(dataset)
    df = json_normalize(data=json, record_path=["records"], errors="ignore",)
    filtered_df = df.filter(items=COLUMNS[dataset])
    return filtered_df
