Skip to content

Commit

Permalink
nest-async for Spyder compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
charwick committed Jul 23, 2021
1 parent 2445fc7 commit d98a750
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion helipad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from helipad.agent import *
import helipad.utility as utility

__version__ = "1.3.1"
__version__ = "1.3.2"
14 changes: 11 additions & 3 deletions helipad/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="helipad",
version="1.3.1",
version="1.3.2",
author="C Harwick",
author_email="[email protected]",
description="An agent-based modeling framework for Python with a shallow learning curve and powerful visualization capabilities.",
Expand Down

0 comments on commit d98a750

Please sign in to comment.