You should read python-bridge repository and follow the examples before you read on this post.
The post about JavaScirpt async programming will help you to understand how to use it greatly.
If you just want to see the entire code first, you can find the end result of this post at pyscript Repository.
Table of Contents
- How python-bridge works
- How to improve the example with async await
1. How python-bridge works
I hope you already invested your time to read the documentation for python-bridge.
The main example for that is
If you haven't yet please install the python-bridge package first with these and make a file with the code snippet below.
If you play with it for a while with
node py.js, You will find that the main API of it are
like they were used above.
If you have both Node and Python installed in your machine and tested it, you will find it work without problem.
It is also important to notice that we should use
To do that you have to use
$pip -m venv <yourproject> and
$source activate to activate that virtual env and install the dependencies you want to test first and should be at the same folder where your package.json is located.
2. How to improve the example with async await
async await syntax like the example below.
You can see that the example from the previous code became more readable with async await.
We could also separate each value from Python and save it as separate variables.
You can test it with $node py.js and hope it would work well.
I think that you don't need explanation for how async await works here. I hope you already read one that explains it best.
With the above example, you just need await keyword whenever you want to save data from Python as variables. Use them whenever you want later.
With async before function name and await for value from Python Virtual Machine We can pass datas easily between Python and JavaScirpt and use both in the same .js file with python-bridge.
You can compare to use
node py.js or test it at your Python console manually similar to the code below or use a website if you can find it.
In the previous part, we learnt how to use async await to make the example from the site more usable. But, you can use them also with some well known packages and make the example useful.
So we will include some well-known Python packages in this part.
Let me show you the code first. To use them, you have to install them first and should be at the right location to use test this while venv is activated correctly for the Python packages.
Update your py.js and
$node py.js to test it work.
While playing with this package, I found that everything wouldn't work well magically with the help from the package author.
We should invest our time to find how to use them for our own project.
2. We test Numpy and Pandas would really work or not. You can see that they work and find that you need to use
- We test various API of pyautogui here. I want you to test it in your own machine. You will see that your mouse, keyboard and screenshot all work well. You can use
pyalso in some cases when you can use
The packages and name used here is not important and its your turn to find get the best out of them.
The code used here is just for a prototype. You will be able to find a better way to compare the speed if you are ready to invest your time for.
So you may have found it more useful than me with the example before if you use Python a lot.
You can save the code snippet below and test it with
For the main purpose of the post is not test and compare speed, I won't find test examples that works with details. If you want, you can make it better.
It was also a trial for me to be more familiar with Python code.
If you liked the post, please share it with others. More posts are coming Follow me here. I am plan to share more blockchain and other stuffs. I am interested in ETH and POLKADOT.