wbgapi360 0.2.1__py3-none-any.whl → 0.2.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wbgapi360/__init__.py CHANGED
@@ -3,7 +3,8 @@ from .search.engine import SearchEngine
3
3
  from .data.builder import DataBuilder
4
4
  from .ai.agent import DataAgent
5
5
  from .metadata.builder import MetadataBuilder
6
- from .visual import viz
6
+ from .metadata.builder import MetadataBuilder
7
+ # from .visual import viz # LAZY LOADED
7
8
 
8
9
 
9
10
  class API:
@@ -34,13 +35,22 @@ class API:
34
35
 
35
36
  @property
36
37
  def visual(self):
37
- return viz
38
+ try:
39
+ from .visual import viz
40
+ return viz
41
+ except ImportError as e:
42
+ if "seaborn" in str(e) or "matplotlib" in str(e):
43
+ raise ImportError(
44
+ "Optional dependency 'seaborn' not found. "
45
+ "Install with: pip install wbgapi360[visual]"
46
+ ) from e
47
+ raise e
38
48
 
39
49
  async def close(self):
40
50
  if self._client:
41
51
  await self._client.close()
42
52
 
43
- __version__ = "0.2.1"
53
+ __version__ = "0.2.8"
44
54
  __author__ = "Maykol Medrano"
45
55
  __email__ = "mmedrano2@uc.cl"
46
56
  __credits__ = ["Applied Economist Policy Data Scientist"]
wbgapi360/api.py CHANGED
@@ -10,7 +10,9 @@ from typing import List, Dict, Any, Union, Optional
10
10
  from wbgapi360.core.client import Data360Client
11
11
  from wbgapi360.search.engine import SearchEngine
12
12
  from wbgapi360.data.builder import DataBuilder
13
- from wbgapi360.visual.charts import Visualizer
13
+ from wbgapi360.data.builder import DataBuilder
14
+ # from wbgapi360.visual.charts import Visualizer # LAZY LOADED
15
+ from wbgapi360.core.utils import normalize_codes, resolve_economies
14
16
  from wbgapi360.core.utils import normalize_codes, resolve_economies
15
17
  from wbgapi360.core.transformers import DataStandardizer
16
18
  from wbgapi360.core.auditor import DataAuditor
@@ -21,7 +23,9 @@ logger.addHandler(logging.NullHandler())
21
23
 
22
24
  # Singletons for Sync API
23
25
  # _client = Data360Client() -> PREVIOUSLY GLOBAL, NOW INSTANTIATED PER CALL TO FIX ASYNCIO LIFECYCLE
24
- _viz = Visualizer()
26
+ # Singletons for Sync API
27
+ # _client = Data360Client() -> PREVIOUSLY GLOBAL, NOW INSTANTIATED PER CALL TO FIX ASYNCIO LIFECYCLE
28
+ # _viz = Visualizer() -> NOW LAZY LOADED
25
29
 
26
30
  def _run_sync(coro):
27
31
  """
@@ -43,6 +47,8 @@ def _run_sync(coro):
43
47
 
44
48
  # --- INTERNAL ASYNC LOGIC (Replicated from server to decouple) ---
45
49
 
50
+ # --- INTERNAL ASYNC LOGIC (Replicated from server to decouple) ---
51
+
46
52
  async def _async_get_data(
47
53
  indicator: Union[str, List[str]],
48
54
  economies: Union[str, List[str]],
@@ -192,6 +198,23 @@ def get_data(
192
198
 
193
199
  return df
194
200
 
201
+ # Helper for lazy loading visualization
202
+ _viz_instance = None
203
+ def _get_viz():
204
+ global _viz_instance
205
+ if _viz_instance is None:
206
+ try:
207
+ from wbgapi360.visual.charts import Visualizer
208
+ _viz_instance = Visualizer()
209
+ except ImportError as e:
210
+ if "seaborn" in str(e) or "matplotlib" in str(e):
211
+ raise ImportError(
212
+ "Optional dependency 'seaborn' or 'matplotlib' not found. "
213
+ "Please install with: pip install wbgapi360[visual]"
214
+ ) from e
215
+ raise e
216
+ return _viz_instance
217
+
195
218
  def plot(chart_type: str, data: Union[str, pd.DataFrame], title: str = "", subtitle: str = "", **kwargs) -> str:
196
219
  """
197
220
  Generate Financial Times-style chart with editorial aesthetics.
@@ -209,26 +232,29 @@ def plot(chart_type: str, data: Union[str, pd.DataFrame], title: str = "", subti
209
232
  """
210
233
  logger.info(f"Plotting chart type: {chart_type}")
211
234
 
235
+ # Lazy load visualizer
236
+ viz = _get_viz()
237
+
212
238
  # Logic adapted for local dispatch
213
239
  dispatch_table = {
214
- 'trend': _viz.plot_trend,
215
- 'line': _viz.plot_trend,
216
- 'bar': _viz.plot_bar,
217
- 'column': _viz.plot_column,
218
- 'scatter': _viz.plot_scatter,
219
- 'map': _viz.plot_map,
220
- 'map_bubble': _viz.plot_map_bubble,
221
- 'map_diverging': _viz.plot_map_diverging,
222
- 'map_categorical': _viz.plot_map_categorical,
223
- 'dumbbell': _viz.plot_dumbbell,
224
- 'stacked': _viz.plot_stacked_bar,
225
- 'stacked_bar': _viz.plot_stacked_bar,
226
- 'area': _viz.plot_area,
227
- 'heatmap': _viz.plot_heatmap,
228
- 'bump': _viz.plot_bump,
229
- 'treemap': _viz.plot_treemap,
230
- 'donut': _viz.plot_donut,
231
- 'pie': _viz.plot_donut
240
+ 'trend': viz.plot_trend,
241
+ 'line': viz.plot_trend,
242
+ 'bar': viz.plot_bar,
243
+ 'column': viz.plot_column,
244
+ 'scatter': viz.plot_scatter,
245
+ 'map': viz.plot_map,
246
+ 'map_bubble': viz.plot_map_bubble,
247
+ 'map_diverging': viz.plot_map_diverging,
248
+ 'map_categorical': viz.plot_map_categorical,
249
+ 'dumbbell': viz.plot_dumbbell,
250
+ 'stacked': viz.plot_stacked_bar,
251
+ 'stacked_bar': viz.plot_stacked_bar,
252
+ 'area': viz.plot_area,
253
+ 'heatmap': viz.plot_heatmap,
254
+ 'bump': viz.plot_bump,
255
+ 'treemap': viz.plot_treemap,
256
+ 'donut': viz.plot_donut,
257
+ 'pie': viz.plot_donut
232
258
  }
233
259
 
234
260
  func = dispatch_table.get(chart_type)
@@ -252,6 +278,13 @@ def plot(chart_type: str, data: Union[str, pd.DataFrame], title: str = "", subti
252
278
  # Execute the plot function with all kwargs
253
279
  func(data, title=title, subtitle=subtitle, save_path=path, **kwargs)
254
280
 
281
+ # Auto-display in Notebooks (Colab/Jupyter)
282
+ try:
283
+ from IPython.display import Image, display
284
+ display(Image(filename=path))
285
+ except (ImportError, Exception):
286
+ pass
287
+
255
288
  return path
256
289
 
257
290
 
wbgapi360/mcp/server.py CHANGED
@@ -14,7 +14,8 @@ from fastmcp import FastMCP
14
14
  from wbgapi360.core.client import Data360Client
15
15
  from wbgapi360.data.builder import DataBuilder
16
16
  from wbgapi360.search.engine import SearchEngine
17
- from wbgapi360.visual.charts import Visualizer
17
+ from wbgapi360.search.engine import SearchEngine
18
+ # from wbgapi360.visual.charts import Visualizer # LAZY LOADED
18
19
 
19
20
  # Setup logger
20
21
  logger = logging.getLogger("wbgapi360.server")
@@ -25,7 +26,9 @@ mcp = FastMCP("wbgapi360-agent")
25
26
  _client_instance: Optional[Data360Client] = None
26
27
  _search_engine_instance: Optional[SearchEngine] = None
27
28
 
28
- viz = Visualizer() # Single instance for visualization is safe as it's synchronous
29
+ _search_engine_instance: Optional[SearchEngine] = None
30
+
31
+ # viz = Visualizer() # LAZY LOADED
29
32
 
30
33
  def _get_client() -> Data360Client:
31
34
  """Lazy initialize the Data360Client."""
@@ -42,6 +45,23 @@ def _get_search_engine() -> SearchEngine:
42
45
  _search_engine_instance = SearchEngine(client)
43
46
  return _search_engine_instance
44
47
 
48
+ # Helper for lazy loading visualization
49
+ _viz_instance = None
50
+ def _get_viz():
51
+ global _viz_instance
52
+ if _viz_instance is None:
53
+ try:
54
+ from wbgapi360.visual.charts import Visualizer
55
+ _viz_instance = Visualizer()
56
+ except ImportError as e:
57
+ if "seaborn" in str(e) or "matplotlib" in str(e):
58
+ raise ImportError(
59
+ "Optional dependency 'seaborn' not found. "
60
+ "Please install with: pip install wbgapi360[visual]"
61
+ ) from e
62
+ raise e
63
+ return _viz_instance
64
+
45
65
  # --- RAW IMPLEMENTATIONS (Core Functions) ---
46
66
 
47
67
  async def _search_indicators(query: str, limit: int = 10, database_id: str = "WB_WDI") -> List[Dict[str, Any]]:
@@ -387,6 +407,7 @@ def _plot_chart(chart_type: str, data: Union[str, pd.DataFrame], title: str = "C
387
407
  os.close(fd)
388
408
 
389
409
  # 3. Dispatcher Logic (Clean Architecture)
410
+ viz = _get_viz()
390
411
  dispatch_table = {
391
412
  'trend': viz.plot_trend,
392
413
  'line': viz.plot_trend,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wbgapi360
3
- Version: 0.2.1
3
+ Version: 0.2.8
4
4
  Summary: Enterprise-grade Async Python Client for World Bank Data360 API
5
5
  Author: Maykol Medrano
6
6
  Author-email: Maykol Medrano <mmedrano2@uc.cl>
@@ -35,7 +35,7 @@ Dynamic: requires-python
35
35
  [![PyPI version](https://img.shields.io/badge/pypi-v0.2.1-0052CC.svg)](https://pypi.org/project/wbgapi360/)
36
36
  [![Python](https://img.shields.io/badge/python-3.9%2B-blue)](https://www.python.org/downloads/)
37
37
  [![License: MIT](https://img.shields.io/badge/License-MIT-grey.svg)](https://opensource.org/licenses/MIT)
38
- [![Build Status](https://github.com/mmedrano2/mcp_wbgapi360/actions/workflows/ci.yml/badge.svg)](https://github.com/mmedrano2/mcp_wbgapi360/actions)
38
+ [![Build Status](https://github.com/MaykolMedrano/mcp_wbgapi360/actions/workflows/ci.yml/badge.svg)](https://github.com/MaykolMedrano/mcp_wbgapi360/actions)
39
39
  [![Status](https://img.shields.io/badge/status-enterprise%20ready-green)]()
40
40
 
41
41
  **wbgapi360** is a high-performance Python client designed to interact with the World Bank's Data APIs. It bridges the gap between traditional econometric analysis and modern Artificial Intelligence workflows.
@@ -62,18 +62,22 @@ We acknowledge and thank Tim for establishing the developer-friendly patterns th
62
62
 
63
63
  ## Installation
64
64
 
65
- The library is available on PyPI.
65
+ Install using pip:
66
66
 
67
67
  ```bash
68
- # Core installation (Lightweight)
69
68
  pip install wbgapi360
69
+ ```
70
70
 
71
+ **For Visualization Features (Plots & Maps):**
72
+ The visualization module is optional to keep the library lightweight. To use `wb.plot()`, install with extras:
73
+ ```bash
74
+ pip install "wbgapi360[visual]"
75
+ ```
71
76
  # With Visualization support (adds Matplotlib/Seaborn)
72
77
  pip install wbgapi360[visual]
73
78
 
74
79
  # With Mapping support (adds GeoPandas - Heavy dependency)
75
80
  pip install wbgapi360[map]
76
- ```
77
81
 
78
82
  ---
79
83
 
@@ -92,29 +96,36 @@ results = wb.search("inflation") # Returns top 10 matches
92
96
 
93
97
  for r in results[:3]:
94
98
  print(f"[{r['code']}] {r['name']}")
95
- # Output:
96
- # [FP.CPI.TOTL.ZG] Inflation, consumer prices (annual %)
97
- # [FP.CPI.TOTL] Consumer price index (2010 = 100)
98
- # [NY.GDP.DEFL.KD.ZG] Inflation, GDP deflator (annual %)
99
- ```
100
-
101
- ### 2. Robust Data Retrieval
102
- Fetch data with automatic ID correction and intelligent time window handling.
103
-
104
- ```python
105
- # Fetch GDP Growth for USA, China, and Peru (Last 10 years)
99
+ # 1. Fetch GDP Growth (Last 10 years)
106
100
  df = wb.get_data(
107
- indicator="NY.GDP.MKTP.KD.ZG",
101
+ indicator="NY.GDP.MKTP.KD.ZG",
108
102
  economies=["USA", "CHN", "PER"],
109
- years=10,
110
- labels=True, # Adds 'Country Name' column
111
- as_frame=True # Returns pandas DataFrame instead of JSON
103
+ years=10
112
104
  )
113
-
114
105
  print(df.head())
106
+
107
+ # 2. Fetch FDI Data (Last 20 years)
108
+ fdi_df = wb.get_data(
109
+ indicator="BX.KLT.DINV.CD.WD",
110
+ economies=["CHL", "USA", "CHN"],
111
+ years=20
112
+ )
113
+ print("Foreign Direct Investment Data:")
114
+ print(fdi_df.head())
115
+
116
+ # 3. Plot Trend (Financial Times Style)
117
+ # Requires: pip install wbgapi360[visual]
118
+ data = wb.get_data("NY.GDP.PCAP.CD", ["CHL", "PER", "COL", "MEX"], years=15)
119
+
120
+ wb.plot(
121
+ chart_type="trend",
122
+ data=data,
123
+ title="GDP Per Capita Trend",
124
+ subtitle="USD Current"
125
+ )
115
126
  ```
116
127
 
117
- ### 3. Advanced Analysis: Trends & Statistics
128
+ ### 4. Advanced Analysis: Trends & Statistics
118
129
  Quickly assess the economic trajectory of a country without writing boilerplate pandas code.
119
130
 
120
131
  ```python
@@ -1,5 +1,5 @@
1
- wbgapi360/__init__.py,sha256=7BEkh1I7oGv0Afx9y1Bgno-k0g_FO6CwUU2K0HAwpco,1224
2
- wbgapi360/api.py,sha256=TxgFoxwaLTlJugIH0iNFdHVJWgseLb3SRYrapTB-dIA,9508
1
+ wbgapi360/__init__.py,sha256=wK5c-PxlT9xw3ytZ9AJDAh4YAMzduFJRjFhDym8uwro,1655
2
+ wbgapi360/api.py,sha256=l0V10hzCI2tZT1_RXCDBLV1lUbMhEus2V9mye3Lz9TM,10720
3
3
  wbgapi360/cli.py,sha256=DxToczs3yUsSt8vANnMTmB2A3TwAjWBZTY5VJF50-Co,3117
4
4
  wbgapi360/config.py,sha256=iOzQgBcBQ-dZWpP1eVgMeFUw3CPtzoxnsDQ7ySYHjB8,1693
5
5
  wbgapi360/ai/agent.py,sha256=KXZky-VmKTRIqrPe_RYsNUnpUk9sw5KX0GY2nRWcTlU,5267
@@ -9,16 +9,16 @@ wbgapi360/core/models.py,sha256=awiKgADvyCI595-ietE-kOg82PwwsmHZMinR7qn7Nvk,742
9
9
  wbgapi360/core/transformers.py,sha256=zrey9b7S-XHgjOFjK2p-Jjkrw1ijrfX4ypbfZHdpFYw,2931
10
10
  wbgapi360/core/utils.py,sha256=TCQDEcHg_alqA5fFqQlVA6NttatLNyyFKsqv744c-T0,1316
11
11
  wbgapi360/data/builder.py,sha256=2Jlua89FKxtjiYE0I1h5iviDTvrMb5vElvOEBeN48vM,7519
12
- wbgapi360/mcp/server.py,sha256=tAradOdhU5iUavu0eQpRMmYTUByWzikfWdxx6_K7WKY,21271
12
+ wbgapi360/mcp/server.py,sha256=TL87vECObaeIzTp_eJy1H1KC2rIjDWu1o9sIM8tFKhI,21973
13
13
  wbgapi360/metadata/builder.py,sha256=8L05WshpcbmzZFHeGH38pZKt36XsB4-VFMn63Qbrtg8,3149
14
14
  wbgapi360/metadata/iso_mapping.py,sha256=55psZTI53_nMsFmvnN21szvO_gTh6noki5waKr8H5NU,5674
15
15
  wbgapi360/metadata/resolver.py,sha256=YNZOcbCnlJ3EJ-dbgTIUitLgSwcmLV3Oz63OaSX1Ozk,4957
16
16
  wbgapi360/search/engine.py,sha256=zTS1We-07-La1DjEkMLeKbIrNbeqbiV6fdZQpzGnOT8,5970
17
17
  wbgapi360/visual/__init__.py,sha256=94LZyoNR1OSWdItQ1CwgeULnwmMN6HSJNHf0oom2970,25
18
18
  wbgapi360/visual/charts.py,sha256=hZSUDbGnRe95LwQUHEERhSqAvvRrIeO6kS2T_YAkyZo,46773
19
- wbgapi360-0.2.1.dist-info/licenses/LICENSE,sha256=A260AC9u7jC7SfQOJ21JOQYqsIJUtgC-6iM2dm3BsNw,1092
20
- wbgapi360-0.2.1.dist-info/METADATA,sha256=G-hG-_xog8MVxAHHJoPAbnmD_YOlX1CsyrnUZGeNtko,8604
21
- wbgapi360-0.2.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
22
- wbgapi360-0.2.1.dist-info/entry_points.txt,sha256=9nNopsIlKEKw9rNe_6zW-ERL6fWu6eXymmKLBIZzzqI,49
23
- wbgapi360-0.2.1.dist-info/top_level.txt,sha256=q3vycsvqQsLZqAFyCzrPw392QV6wAYbVamr39Z-n2MQ,10
24
- wbgapi360-0.2.1.dist-info/RECORD,,
19
+ wbgapi360-0.2.8.dist-info/licenses/LICENSE,sha256=A260AC9u7jC7SfQOJ21JOQYqsIJUtgC-6iM2dm3BsNw,1092
20
+ wbgapi360-0.2.8.dist-info/METADATA,sha256=iDmB3t2ofOaILceLauQ3Y4Zji6S_iMQa26WgpeT1Tts,8802
21
+ wbgapi360-0.2.8.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
22
+ wbgapi360-0.2.8.dist-info/entry_points.txt,sha256=9nNopsIlKEKw9rNe_6zW-ERL6fWu6eXymmKLBIZzzqI,49
23
+ wbgapi360-0.2.8.dist-info/top_level.txt,sha256=q3vycsvqQsLZqAFyCzrPw392QV6wAYbVamr39Z-n2MQ,10
24
+ wbgapi360-0.2.8.dist-info/RECORD,,