Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
!! Warning  !! Don't waste your time in the event that you stumble across this repo. This is a personal learning project for me... a noob... I don't know if it is an original idea, a novel approach, or will even accomplish the intended purpose. This is the first project that I have attempted to use Poetry, which was a total PITA. Additionally, I had to manually load the files into the repository because I have some issues with Git that I am still working out. Worst of all, I really suck at coding at the moment. I'm making this public for two reason. First, I wanted to see if there were any differences in uploading Private and Public repos (I did upload my first project ever the other day set to 'Private'). Secondly, I felt like it was kind of a jerk move to only upload private stuff to an open source platform. Then again, uploading totally shit noob projects could be a faux pas. I simply haven't learned that yet.  All of that said, one must start somewhere... And I am starting here.
  • Loading branch information
AlexJ-StL authored Jul 17, 2024
1 parent d8ddca9 commit 1414110
Show file tree
Hide file tree
Showing 10 changed files with 2,077 additions and 0 deletions.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# FRED Data Visualization App

This Streamlit app creates visualizations using data from the Federal Reserve Bank of St. Louis (FRED, ALFRED, and Maps APIs).

## Setup

1. Perquisites

a. Ensure you have a FRED API which can be obtained by creating an account at https://fredaccount.stlouisfed.org/login/secure/ and requesting an API from the menu on the left hand side of the page.

b. Python 3.7+ installed on your system.

2. Install Poetry if you haven't already:
```
pip install poetry
```

3. Clone this repository:
```
git clone <repository-url>
cd fred-visualization-app
```

4. Install the project dependencies using Poetry:
```
poetry install
```

5. Activate the virtual environment created by Poetry:
```
poetry shell
```

6. Create an `.env` file in the project root and add your FRED API key:
```
FRED_API_KEY=your_api_key_here
```

## Running the App

To run the Streamlit app, use the following command within the Poetry shell:

```
streamlit run app.py
```

The app should open in your default web browser. If it doesn't, you can access it at `http://localhost:8501`.

## Using the App

1. Enter a FRED Series ID in the sidebar (e.g., 'GDP' for Gross Domestic Product).
2. Select start and end dates for the data you want to visualize.
3. Click 'Fetch Data' to retrieve and visualize the data.
4. Use the ALFRED section to view historical releases of a specific series.
5. Use the Maps section to view geographical data (note: this is a simplified representation and doesn't include actual map visualizations).

## Available Data Sources

- FRED (Federal Reserve Economic Data)
- ALFRED (Archival Federal Reserve Economic Data)
- FRED Maps (Geographical Economic Data)

## Troubleshooting

If you encounter any issues:

1. Ensure you have the latest version of Poetry installed:
```
pip install --upgrade poetry
```

2. Try removing the existing virtual environment and reinstalling dependencies:
```
poetry env remove python
poetry install
```

3. If you're still having issues with specific packages, you can try updating them individually:
```
poetry update package_name
```

4. Verify that your API key is correct in the `.env` file.

5. Check your internet connection, as the app requires access to the FRED API.

6. If you're experiencing issues related to Streamlit or its dependencies, you can try using a specific Python version that's known to work well with the current Streamlit version:
```
poetry env use python3.9
poetry install
```

For more information on available FRED series IDs and API usage, visit: https://fred.stlouisfed.org/docs/api/fred/

## License

This project is open source and available under the [MIT License](LICENSE).
Empty file added __init__.py
Empty file.
Empty file.
138 changes: 138 additions & 0 deletions fred-visualization-app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""
FRED Data Visualization App
This module provides a Streamlit-based web application for visualizing
Federal Reserve Economic Data (FRED).
It allows users to select and compare different economic indicators,
apply various aggregations,
and generate insights from the data.
"""
import os
import pandas as pd
import plotly.graph_objects as go
import streamlit as st
from dotenv import load_dotenv
from fredapi import Fred

from .fred_categories import category_component, fetch_fred_categories

# Load environment variables
load_dotenv()

# Initialize FRED API
fred = Fred(api_key=os.getenv('FRED_API_KEY'))

# Streamlit configuration
st.set_page_config(layout="wide")
st.title('FRED Data Visualization App')

# --- Data Selection Sidebar ---
st.sidebar.header('Data Selection')

# 1. Category Selection (using memoization for efficiency)
@st.cache_data
def get_fred_categories():
return fetch_fred_categories()


categories = get_fred_categories()
category_names = [cat['name'] for cat in categories]
selected_category_name = st.sidebar.selectbox("Select Category", options=category_names)
selected_category_id = next(
cat['id'] for cat in categories if cat['name'] == selected_category_name
)

# 2. Series Selection (using memoization for efficiency)
@st.cache_data
def get_series_list(category_id):
return fred.get_series_in_category(category_id)


series_list = get_series_list(selected_category_id)
series_id = st.sidebar.selectbox('Select FRED Series:', options=series_list)

# 3. Comparison Series (Conditional)
enable_comparison = st.sidebar.checkbox('Compare with another dataset')
series_id2 = None
if enable_comparison:
series_id2 = st.sidebar.selectbox(
'Select second FRED Series:',
options=series_list,
key='series2' # Important for widget state
)

# 4. Date Range Selection
start_date = st.sidebar.date_input('Start Date')
end_date = st.sidebar.date_input('End Date')

# Aggregation option
aggregation = st.sidebar.selectbox(
'Aggregation',
options=['None', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']
)

# Visualization options
viz_type = st.sidebar.selectbox(
'Visualization Type',
options=['Line', 'Bar', 'Scatter', '3D Scatter', 'Area']
)

@st.cache_data
def get_fred_data(series_id, start_date, end_date):
"""
Fetch data from FRED API for a given series and date range.
Args:
series_id (str): The FRED series identifier
start_date (str): Start date for the data range
end_date (str): End date for the data range
Returns:
pd.DataFrame: DataFrame containing the fetched data
"""
try:
data = fred.get_series(series_id, start_date, end_date)
df = pd.DataFrame(data).reset_index()
df.columns = ['Date', 'Value']
return df
except fredapi.fred.HTTPError as e:
st.error(f"Error fetching data from FRED: {str(e)}")
return pd.DataFrame(columns=['Date', 'Value'])
except Exception as e:
st.error(f"An unexpected error occurred: {str(e)}")
return pd.DataFrame(columns=['Date', 'Value'])


def aggregate_data(df, agg_type):
"""
Aggregate data based on the specified aggregation type.
Args:
df (pd.DataFrame): Input DataFrame
agg_type (str): Aggregation type (
'None', 'Weekly', 'Monthly', 'Quarterly', 'Yearly'
)
Returns:
pd.DataFrame: Aggregated DataFrame
"""
df['Date'] = pd.to_datetime(df['Date'])

resampling_options = {
'Weekly': 'W',
'Monthly': 'M',
'Quarterly': 'Q',
'Yearly': 'Y'
}

if agg_type in resampling_options:
return df.resample(
""" How to use this app:
1. Select a category and series from the sidebar
2. Choose to compare with another dataset (optional)
3. Select start and end dates
4. Choose aggregation and visualization type
5. Click 'Fetch Data' to visualize
6. Explore insights below the visualization
7. Use the FRED Categories section for more information"""
)
48 changes: 48 additions & 0 deletions fred-visualization-app/fred_categories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import requests
from dash import html, dcc
import dash_bootstrap_components as dbc

FRED_API_KEY = 'your_fred_api_key_here'
# Replace with your actual FRED API key

def fetch_fred_categories():
url = f"https://api.stlouisfed.org/fred/category/children?category_id=0&api_key={FRED_API_KEY}&file_type=json"
response = requests.get(url)
if response.status_code == 200:
return response.json()['categories']
else:
return []


def create_category_list():
categories = fetch_fred_categories()
return html.Div([
html.H3("FRED Categories"),
dbc.ListGroup([
dbc.ListGroupItem([
html.Div(f"{category['name']} (ID: {category['id']})"),
html.A(
"View on FRED",
href=f"https://fred.stlouisfed.org/categories/{category['id']}",
target="_blank"
)
]) for category in categories
]),
html.Div([
html.A("View all categories on FRED", href="https://fred.stlouisfed.org/categories", target="_blank"),
html.Br(),
html.A("Search for FRED series", href="https://fred.stlouisfed.org/tags/series", target="_blank")
], className="mt-3")
])


def category_component():
return html.Div([
html.H2("FRED Categories and Series"),
dbc.Input(
id="category-search",
type="text",
placeholder="Search categories..."
),
html.Div(id="category-list", children=[create_category_list()])
])
Loading

0 comments on commit 1414110

Please sign in to comment.