Sebastien Peytrignet
commited on
Commit
•
f89fe21
1
Parent(s):
2b8f481
chore: Update dependencies and add utility functions for querying and aggregating data
Browse files- .gitignore +160 -0
- app.py +5 -20
- utils.py +65 -0
.gitignore
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
# .env
|
124 |
+
# .venv
|
125 |
+
# env/
|
126 |
+
# venv/
|
127 |
+
# ENV/
|
128 |
+
# env.bak/
|
129 |
+
# venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import folium
|
4 |
from streamlit_folium import folium_static
|
5 |
from pyeuropeana import apis
|
6 |
from dotenv import load_dotenv
|
|
|
7 |
import os
|
8 |
|
9 |
# Load environment variables
|
@@ -14,25 +16,8 @@ if 'EUROPEANA_API_KEY' not in os.environ:
|
|
14 |
st.error("EUROPEANA_API_KEY is not set in the environment variables. Please set it and restart the app.")
|
15 |
st.stop()
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
query='pl_wgs84_pos_lat:(*)',
|
20 |
-
qf=f'DATA_PROVIDER:"{provider_name}"',
|
21 |
-
rows=rows
|
22 |
-
)
|
23 |
-
|
24 |
-
myquery_df = pd.DataFrame(myquery["items"], columns=['edmPlaceLatitude', 'edmPlaceLongitude', 'id', 'country', 'dataProvider', 'dcCreator'])
|
25 |
-
|
26 |
-
def extract_single(x):
|
27 |
-
return x[0] if isinstance(x, list) and len(x) > 0 else x
|
28 |
-
|
29 |
-
for col in ['edmPlaceLatitude', 'edmPlaceLongitude', 'country', 'dataProvider', 'dcCreator']:
|
30 |
-
myquery_df[col] = myquery_df[col].apply(extract_single)
|
31 |
-
|
32 |
-
myquery_df['edmPlaceLatitude'] = pd.to_numeric(myquery_df['edmPlaceLatitude'], errors='coerce')
|
33 |
-
myquery_df['edmPlaceLongitude'] = pd.to_numeric(myquery_df['edmPlaceLongitude'], errors='coerce')
|
34 |
-
|
35 |
-
return myquery_df
|
36 |
|
37 |
# Set up the Streamlit app
|
38 |
st.title('Europeana Data Explorer')
|
@@ -46,7 +31,7 @@ if st.button('Fetch Data'):
|
|
46 |
# Show loading message
|
47 |
with st.spinner('Fetching data...'):
|
48 |
# Get the data
|
49 |
-
df = get_provider_data(provider_name)
|
50 |
|
51 |
# Display the data
|
52 |
st.subheader(f'Data from {provider_name}')
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import geopandas as gpd
|
4 |
import folium
|
5 |
from streamlit_folium import folium_static
|
6 |
from pyeuropeana import apis
|
7 |
from dotenv import load_dotenv
|
8 |
+
from utils import get_provider_data, aggregate_location_counts
|
9 |
import os
|
10 |
|
11 |
# Load environment variables
|
|
|
16 |
st.error("EUROPEANA_API_KEY is not set in the environment variables. Please set it and restart the app.")
|
17 |
st.stop()
|
18 |
|
19 |
+
# Load world map dataset
|
20 |
+
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Set up the Streamlit app
|
23 |
st.title('Europeana Data Explorer')
|
|
|
31 |
# Show loading message
|
32 |
with st.spinner('Fetching data...'):
|
33 |
# Get the data
|
34 |
+
df = get_provider_data(provider_name, world)
|
35 |
|
36 |
# Display the data
|
37 |
st.subheader(f'Data from {provider_name}')
|
utils.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def get_provider_data(provider_name, world_gdf, rows=1000):
|
2 |
+
"""
|
3 |
+
Query Europeana API for a specific data provider and return a processed DataFrame.
|
4 |
+
|
5 |
+
Args:
|
6 |
+
provider_name (str): Name of the data provider to query
|
7 |
+
world_gdf (GeoDataFrame): World map GeoDataFrame for spatial join
|
8 |
+
rows (int): Number of rows to retrieve (default 1000)
|
9 |
+
|
10 |
+
Returns:
|
11 |
+
pandas.DataFrame: Processed DataFrame with the queried data, including object location
|
12 |
+
"""
|
13 |
+
# Query the Europeana API
|
14 |
+
myquery = apis.search(
|
15 |
+
query='pl_wgs84_pos_lat:(*)',
|
16 |
+
qf=f'DATA_PROVIDER:"{provider_name}"',
|
17 |
+
rows=rows
|
18 |
+
)
|
19 |
+
|
20 |
+
# Create initial DataFrame
|
21 |
+
myquery_df = pd.DataFrame(myquery["items"], columns=['edmPlaceLatitude', 'edmPlaceLongitude', 'id', 'country', 'dataProvider', 'dcCreator'])
|
22 |
+
|
23 |
+
# Function to extract single value from list or return original value
|
24 |
+
def extract_single(x):
|
25 |
+
return x[0] if isinstance(x, list) and len(x) > 0 else x
|
26 |
+
|
27 |
+
# Apply extraction to relevant columns
|
28 |
+
for col in ['edmPlaceLatitude', 'edmPlaceLongitude', 'country', 'dataProvider', 'dcCreator']:
|
29 |
+
myquery_df[col] = myquery_df[col].apply(extract_single)
|
30 |
+
|
31 |
+
# Convert latitude and longitude to float type
|
32 |
+
myquery_df['edmPlaceLatitude'] = pd.to_numeric(myquery_df['edmPlaceLatitude'], errors='coerce')
|
33 |
+
myquery_df['edmPlaceLongitude'] = pd.to_numeric(myquery_df['edmPlaceLongitude'], errors='coerce')
|
34 |
+
|
35 |
+
# Create a GeoDataFrame from the DataFrame
|
36 |
+
gdf = gpd.GeoDataFrame(
|
37 |
+
myquery_df,
|
38 |
+
geometry=gpd.points_from_xy(myquery_df.edmPlaceLongitude, myquery_df.edmPlaceLatitude),
|
39 |
+
crs="EPSG:4326"
|
40 |
+
)
|
41 |
+
|
42 |
+
# Perform spatial join
|
43 |
+
gdf_with_country = gpd.sjoin(gdf, world_gdf[['geometry', 'name']], how='left', op='within')
|
44 |
+
|
45 |
+
# Add the new column to the original DataFrame
|
46 |
+
myquery_df['object_location'] = gdf_with_country['name']
|
47 |
+
|
48 |
+
# Fill NaN values (points that don't fall within any country) with "Unknown"
|
49 |
+
myquery_df['object_location'] = myquery_df['object_location'].fillna("Unknown")
|
50 |
+
|
51 |
+
return myquery_df
|
52 |
+
|
53 |
+
def aggregate_location_counts(df):
|
54 |
+
"""
|
55 |
+
Aggregate the data by object_location and get counts.
|
56 |
+
|
57 |
+
Args:
|
58 |
+
df (pandas.DataFrame): DataFrame containing 'object_location' column
|
59 |
+
|
60 |
+
Returns:
|
61 |
+
pandas.DataFrame: DataFrame with object locations and their counts, sorted by count
|
62 |
+
"""
|
63 |
+
location_counts = df['object_location'].value_counts().reset_index()
|
64 |
+
location_counts.columns = ['object_location', 'count']
|
65 |
+
return location_counts.sort_values('count', ascending=False)
|