ipywidget button with on_click utilizing async functions of fabric-sdk-py. Example to make it work?

Viewed 185

Currently I'm implementing an interface to the Hyperledger Fabric using Jupyter and fabric-sdk-py. I want to submit a transaction and/or evaluate tranactions using Button ipywidget (simple example is):

def _check_id(b):
    x = await contract.evaluate_transaction('queryUnprotectedEntry', [_id_info.value], user)
_check_id_button.on_click(_check_id)

Of course this doesn't work since await is called outside of an async function. If I use async def _check_id(b) I get problems because _check_id' was never awaited.

If I use asyncio.run or asyncio.get_event_loop().run_until_complete, it doesn't work as well with This event loop is already running

What would be the way for a button to execute an async function using on_click?

1 Answers

Not sure currently if this is the best solution but it works. To make everything work in the beginning of the module I used:

import nest_asyncio
nest_asyncio.apply()

afterwards I'm able to use the following:

def _check_id(b):
    loop = asyncio.get_event_loop()
    result = loop.run_until_complete(contract.evaluate_transaction('queryUnprotectedEntry', [_id_info.value], user)
_check_id_button.on_click(_check_id)
Related