From d98a75034aec51bfe8a5e57a64f54c30da349105 Mon Sep 17 00:00:00 2001 From: charwick <1117120+charwick@users.noreply.github.com> Date: Thu, 22 Jul 2021 23:04:40 -0400 Subject: [PATCH] nest-async for Spyder compatibility --- README.md | 15 ++++++++------- helipad/__init__.py | 2 +- helipad/model.py | 14 +++++++++++--- setup.py | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index cdb9674..e026c2b 100644 --- a/README.md +++ b/README.md @@ -32,16 +32,17 @@ The included [bootstrap model](https://github.com/charwick/helipad/blob/master/s Helipad requires Python 3.7 or higher. The following libraries are also required: -* [Matplotlib](https://matplotlib.org/) (for visualization) -* [Pandas](https://pandas.pydata.org/) (for data collection) +* [Matplotlib](https://matplotlib.org/) for visualization +* [Pandas](https://pandas.pydata.org/) for data collection The following libraries are optional but recommended: -* [Jupyter](https://jupyter.org/), [Ipywidgets](https://pypi.org/project/ipywidgets/), and [ipympl](https://github.com/matplotlib/ipympl) (to run Helipad in Jupyter notebooks) -* [NetworkX](http://networkx.github.io/) (for network analysis and spatial visualization) -* [PMW](https://pypi.org/project/Pmw/) (for tooltips in the Tkinter GUI) -* [Readline](https://pypi.org/project/readline/) and Code (for the debug console) -* [Pyobjc](https://pypi.org/project/pyobjc/) (for Mac interface niceties) +* [Jupyter](https://jupyter.org/), [Ipywidgets](https://pypi.org/project/ipywidgets/), and [ipympl](https://github.com/matplotlib/ipympl) to run Helipad in Jupyter notebooks +* [NetworkX](http://networkx.github.io/) for network analysis and spatial visualization +* [PMW](https://pypi.org/project/Pmw/) for tooltips in the Tkinter GUI +* [Readline](https://pypi.org/project/readline/) and Code for the debug console +* [Pyobjc](https://pypi.org/project/pyobjc/) for Mac interface niceties +* [Nest-asyncio](https://pypi.org/project/nest-asyncio/) to run Helipad from Spyder ## Version History diff --git a/helipad/__init__.py b/helipad/__init__.py index 503b5d0..6afd6ae 100644 --- a/helipad/__init__.py +++ b/helipad/__init__.py @@ -2,4 +2,4 @@ from helipad.agent import * import helipad.utility as utility -__version__ = "1.3.1" \ No newline at end of file +__version__ = "1.3.2" \ No newline at end of file diff --git a/helipad/model.py b/helipad/model.py index c2a5d38..7bc5e73 100644 --- a/helipad/model.py +++ b/helipad/model.py @@ -429,6 +429,14 @@ def reporter(model): return param.get(item) for param in self.allParams: if not param.runtime: param.disable() #Disable parameters that can't be changed during runtime + #Patch our async functions for compatibility with Spyder's event loop + if isIpy(True) and not isIpy(): + try: + import nest_asyncio + nest_asyncio.apply() + except: + raise ImportError('nest_asyncio is required to run Helipad from Spyder.') + self.hasModel = True self.doHooks('modelPostSetup', [self]) @@ -538,7 +546,7 @@ async def run(self): self.doHooks('visualRefresh', [self, self.visual]) elif getattr(self, 'cpanel', None): - if isIpy(True): await asyncio.sleep(0.001) #Listen for keyboard input + if isIpy(): await asyncio.sleep(0.001) #Listen for keyboard input else: self.cpanel.parent.update() #Make sure we don't hang the interface if plotless # Performance indicator @@ -567,8 +575,8 @@ def start(self, *args): #that the statement in the try block doesn't get executed…? with warnings.catch_warnings(): warnings.simplefilter("ignore") - if not isIpy(True): asyncio.run(self.run()) #If Tkinter, it needs an event loop - else: asyncio.ensure_future(self.run()) #If Jupyter, it already has an event loop + if isIpy(): asyncio.ensure_future(self.run()) #If Jupyter, it already has an event loop + else: asyncio.run(self.run()) #If Tkinter, it needs an event loop def stop(self, *args): self.running = False diff --git a/setup.py b/setup.py index eb3c654..4c3022e 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="helipad", - version="1.3.1", + version="1.3.2", author="C Harwick", author_email="cameron@cameronharwick.com", description="An agent-based modeling framework for Python with a shallow learning curve and powerful visualization capabilities.",