2

Im new to pyscript and i wan to use web3.py in my index page

This here is my index.html page

<body>
   
   <py-env>
      - web3
   </py-env>
 <py-script >
   import web3

   bsc = 'https://bsc-dataseed.binance.org/'    
   web3 = Web3(Web3.HTTPProvider(bsc))
   print(web3.isConnected())
 </py-script>

</body>

When I run this file the page is only loading but nothing is being displayed on my site.

I have installed web3 and I have tried to import it as

<py-env>
      - web3==5.30.0
   </py-env>

same error as above

TylerH
  • 20,799
  • 66
  • 75
  • 101
An droid
  • 47
  • 5
  • Hello @Android, and welcome to StackOverflow! You mentioned "same error as above". What do you mean by the error? Do you mean that nothing was displayed on the page? Also, I believe that pyscript is still under active development, and socket (and therefore perhaps Web3) doesn't work because of restrictions of running things on the web. – David Jul 30 '22 at 15:31

1 Answers1

2

The package web3 is not compatible with PyScript because the package depends on websockets which is not a pure Python 3 wheel and cannot be loaded by pyodide.

Any package that depends upon the TCP Sockets I/O APIs is not supported within a browser due to sandbox security limitations.

The solution is to ask the web3 team to update their package, or perhaps do the updates yourself and contribute the changes.

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • can't we just run JS methods (to extract Metamask accounts and so on) then use normal python codes on our machine using pyscript? – rsc05 Sep 07 '22 at 12:23
  • @Anonymous - please create a new question. – John Hanley Sep 07 '22 at 19:01
  • I wrote it here https://stackoverflow.com/questions/73633342/how-to-request-eth-requestaccounts-in-pyscript – rsc05 Sep 07 '22 at 19:41
  • I would really appreciate it if you could help me. Also, regarding your answer, If one needs to use web3 I think it would be possible to use flask with PyScript. Where web3 could be run from the server side (I think) and PyScript could run for the front-end side. – rsc05 Sep 07 '22 at 19:48
  • @Anonymous - if you need help, create a question. https://stackoverflow.com/help/how-to-ask – John Hanley Sep 07 '22 at 20:48