0

I am creating a website which scrapes data from a site and displays that data in my site. I used selenium for scraping and this is purely written in python which converts scraped data to JSON. I used Javascript to fetch data from JSON and display it in HTML.

Now there are two things what I want to ask.

Firstly, how can I run that python file in web browser via web hosting.

Secondly, what I want is that every day my Javascript code run that python file so that whenever website is updated, the updated data is displayed in my website also.

Can anyone guide me how I can run Python file from Javascript. I also viewed Brython but it doesn't run my python(.py). Here's how I used Brython

These are the js files imported in head

<script src="/brython.js"></script>
<script src="/brython_stdlib.js></script>

And here in body tag I give reference to my python file

<body onload="brython()">
     <script type="text/python" src="webScrapper.py">
</body>

But nothing happens.

Hope my question is clear. Thanks in advance for your brainstorming ideas

Hamid Ayub
  • 77
  • 5
  • https://stackoverflow.com/a/45962931/6616963 Follow this for a answer – Yatish Kadam Jan 03 '21 at 09:16
  • Well the answer is You can't directly on browser. https://stackoverflow.com/questions/13175510/call-python-function-from-javascript-code – Ashutosh Kumar Jan 03 '21 at 09:37
  • you can't run Python directly in web browser - you have to convert it to JavaScript - ie. using `brython` or `transcrypt` or `rapydscript`. But not all code can be converted to JavaScript (some modules need access to C/C++ libraries) and your idea will not work. You should rather run Python on server (ie. Flask) and send only results as `HTML` or `JSON`. And browser should use `JavaScript` to get data from server and update HTML in browser. And JavaScript will have to periodically ask server if there are new data - server can't send it if browser doesn't ask for it. – furas Jan 03 '21 at 10:57

1 Answers1

2

Well, I don't think you can run python script on a web browser using js. But what you can do is spawn python as a child process from a node js server. I have done something similar. I ran ruby script from a nodejs.

You can find the code here. you just have to make minor changes

https://github.com/thealpha93/spawn-child-process.git

thealpha93
  • 780
  • 4
  • 15