-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 99993ae
Showing
13 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 tony hollaar | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Streamlit Connection API | ||
The Streamlit Connection API is a custom-built Python package that allows you to easily interact with the U.S. Bureau of Labor Statistics (BLS) API and retrieve data as pandas dataframes. | ||
|
||
## Installation | ||
|
||
To install the Streamlit Connection API, simply run the following command: | ||
```python | ||
pip install streamlit-bls-connection | ||
``` | ||
|
||
## Example Streamlit API: | ||
|
||
```python | ||
import streamlit as st | ||
from streamlit_bls_connection import BLSConnection | ||
|
||
# Step 1: Setup connection to US Bureau of Labor Statistics | ||
connection = BLSConnection("bls_connection") | ||
|
||
# Step 2: Define Input parameters for the API call | ||
# Tip: one or multiple Series ID's* can be retrieved | ||
seriesids_list = ['APU000074714', 'APU000072610'] | ||
start_year_str = '2014' # start of date range | ||
end_year_str = '2023' # end of date range | ||
|
||
# Step 3: Fetch data using the custom connection | ||
dataframes_dict = connection.query(seriesids_list, start_year_str, end_year_str) | ||
|
||
# Step 4: Create dataframes | ||
gas_df = dataframes_dict['APU000074714'] | ||
electricity_df = dataframes_dict['APU000072610'] | ||
|
||
# Step 5: Show Dataframes in Streamlit | ||
st.dataframe(gas_df, electricity_df) | ||
``` | ||
|
||
## License | ||
This project is licensed under the MIT License. See the LICENSE file for details. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from setuptools import setup, find_packages | ||
|
||
with open("README.md", "r", encoding="utf-8") as fh: | ||
long_description = fh.read() | ||
|
||
setup( | ||
name='streamlit_bls_connection', | ||
version='0.5', | ||
license='MIT', | ||
description='A package to fetch Bureau of Labor Statistics data using Streamlit', | ||
author='Tony Hollaar', | ||
author_email='[email protected]', | ||
packages=find_packages(), | ||
keywords = ['streamlit', 'api', 'bls'] | ||
url = 'https://github.com/tonyhollaar/', | ||
install_requires=[ | ||
'streamlit', | ||
'requests', | ||
'pandas', | ||
], | ||
classifiers=[ | ||
'Development Status :: 4 - Beta', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package | ||
'Intended Audience :: Developers', # Define that your audience are developers | ||
'Topic :: Software Development :: Build Tools', | ||
'License :: OSI Approved :: MIT License', # Again, pick a license | ||
'Programming Language :: Python :: 3.7', #Specify which pyhton versions that you want to support | ||
'Programming Language :: Python :: 3.8', | ||
'Programming Language :: Python :: 3.9', | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Metadata-Version: 2.1 | ||
Name: streamlit-bls-connection | ||
Version: 0.4 | ||
Summary: A package to fetch Bureau of Labor Statistics data using Streamlit | ||
Author: Tony Hollaar | ||
Author-email: [email protected] | ||
License-File: LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
LICENSE | ||
README.md | ||
setup.py | ||
streamlit_bls_connection/__init__.py | ||
streamlit_bls_connection/streamlit_bls_connection.py | ||
streamlit_bls_connection.egg-info/PKG-INFO | ||
streamlit_bls_connection.egg-info/SOURCES.txt | ||
streamlit_bls_connection.egg-info/dependency_links.txt | ||
streamlit_bls_connection.egg-info/requires.txt | ||
streamlit_bls_connection.egg-info/top_level.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
streamlit | ||
requests | ||
pandas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
streamlit_bls_connection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from streamlit_bls_connection.bls_connection import BLSConnection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Mon Jul 24 15:12:23 2023 | ||
@author: tholl | ||
""" | ||
import streamlit as st | ||
from streamlit.connections import ExperimentalBaseConnection | ||
import requests | ||
import pandas as pd | ||
import json | ||
|
||
class BLSConnection(ExperimentalBaseConnection): | ||
def __init__(self, connection_name, **kwargs): | ||
super().__init__(connection_name=connection_name, **kwargs) | ||
# Load any connection-specific configuration or credentials here if needed. | ||
|
||
def _connect(self, **kwargs): | ||
# Implement the connection setup here. | ||
# We don't need to explicitly set up a connection in this case, | ||
# as we'll be making direct API calls in the methods below. | ||
pass | ||
|
||
def fetch_data(self, seriesids, start_year, end_year): | ||
dataframes_dict = {} | ||
headers = {'Content-type': 'application/json'} | ||
|
||
# iterate over one or more timeseries | ||
for series_id in seriesids: | ||
# create empty list to save data for the current seriesId | ||
parsed_data = [] | ||
# set the variable to retrieve from the public dataset | ||
data = json.dumps({"seriesid": [series_id], "startyear": start_year, "endyear": end_year}) | ||
p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data=data, headers=headers) | ||
json_data = json.loads(p.text) | ||
|
||
# iterate over the json file | ||
for series in json_data['Results']['series']: | ||
# iterate over the list of lists that contains the data | ||
for item in series['data']: | ||
# within each list retrieve the year, period, value and footnotes | ||
year = item['year'] | ||
period = item['period'] | ||
value = item['value'] | ||
footnotes = "" | ||
for footnote in item['footnotes']: | ||
if footnote: | ||
footnotes = footnotes + footnote['text'] + ',' | ||
parsed_data.append([series_id, year, period, value, footnotes[0:-1]]) | ||
|
||
df = pd.DataFrame(parsed_data, columns=['seriesID', 'year', 'period', 'value', 'footnotes']) | ||
df['value'] = pd.to_numeric(df['value']) | ||
df['month'] = pd.to_numeric(df['period'].replace({'M': ''}, regex=True)) | ||
df['date'] = df['month'].map(str) + '-' + df['year'].map(str) | ||
df['date'] = pd.to_datetime(pd.to_datetime(df['date'], format='%m-%Y').dt.strftime('%m-%Y')) | ||
df = df.sort_values(by='date', ascending=True) | ||
df['perct_change_value'] = df['value'].pct_change() | ||
|
||
# add the dataframe to the dictionary with the seriesid as the key | ||
dataframes_dict[series_id] = df | ||
return dataframes_dict | ||
|
||
@staticmethod | ||
@st.cache_data(ttl="1d") # Cache the data for one day (24 hours) | ||
def query(series_id, start_year, end_year): | ||
# This method will be called by the Streamlit app to retrieve data using the custom connection. | ||
# You can implement any caching logic or other data processing here. | ||
connection = BLSConnection("bls_connection") | ||
try: | ||
return connection.fetch_data(series_id, start_year, end_year) | ||
except KeyError: | ||
with st.sidebar: | ||
st.error("😒 **Error**: Failed to fetch latest data. Daily query limit is exceeded, retrieving stored data from backup source.") | ||
#st.stop() # Stop the app execution and display the error message to the user | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Inside of setup.cfg | ||
[metadata] | ||
description-file = README.md |