Run external python script by clicking a button in the Browser

441 views
Skip to first unread message

eydz zdye

unread,
Apr 4, 2021, 9:27:09 PM4/4/21
to brython
Hi,

     I have no luck in finding a good example to run an external python script (ex: hello_ world.py) by clicking a button in HTML browser via Brython. 

Can someone share an example, please?

Thanks is advance. 

Edison

Francisco Javier Vazquez Umbria

unread,
Apr 5, 2021, 2:47:19 AM4/5/21
to bry...@googlegroups.com
I don't have any example at hand, but you need...

- Include the brython.js and stdlib in your HTML
- In your python script section of the HTML, import the function you want to call from hello_world.py
- again, in your python script section of the HTML, bind the function with the 'click' event of the button.

Use the developer functions of your browser (usually, F12 key) to view any network request/error while brython is importing the hello_world.py file. For beginers, the HTML and the PY file must be in the same folder. it's possible to put the HTML and the PY files in different folders, but for now, maintain it in the same folder.

as you can see, the trick is to think in the python script of the HTML as the 'main' function of your app.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/3ffb9262-010f-4045-9795-4c17096de950n%40googlegroups.com.

Ian Sparks

unread,
Apr 5, 2021, 4:06:49 AM4/5/21
to brython
Ray has been working on a starter template and example that does exactly this. Loads an external python script and attaches a "hello world" message to a button in the html page.

Code here:


see it in action:

Ray Luo

unread,
Apr 5, 2021, 12:05:25 PM4/5/21
to brython
Thanks for promoting our work, Ian. :-)

I thought the keyword "external" in OP's question would mean a Python script hosted on a different website with different domain. I haven't tried that setup. It might still work if the 3rd-party site allows CORS. Perhaps Edison can clarify.

eydz zdye

unread,
Apr 6, 2021, 10:17:44 AM4/6/21
to brython
Thank you Ray and Ian for the quick responses. 

     The example codes are almost close to my needs. I tried to use them as a template. However, I'm getting an error in my browser console logs. 
The error is  "404 (File not found)" for all the imported modules (like requests, smtplib, paramiko..etc) that are in the external script.

      Actually, my external server_healthcheck.py script is doing complicated data manipulation and has several functions on it. But, the output of the main function will be sent to another html file (output.html). Then, the output.html file will be read by Brython and displayed it to the user in the browser page.

      BTW, the external script runs normally when executed thru the CLI in the main unix server. I just need to have a browser UI so my colleagues will be able to use the health check tool.
I hope there are example codes similar to this.

Ian Sparks

unread,
Apr 6, 2021, 5:47:23 PM4/6/21
to brython
I think I understand what you are trying to do. You have a "health check" python script which checks that some systems are OK by using smtplib to connect to a mail server, requests to check a website or API is working and paramiko to connect via SSH. You want your colleagues to be able to run this check remotely via a web page?

I think it would be easier to set up a small web server on the machine with the healthcheck script using a python webserver framework like flask (or even CGI)  and then have the colleagues request a page that runs the healthcheck code and then returns the html page with the results. They don't need anything more than a browser.

But imagine if you COULD run your healthcheck.py script from code hosted in the browser using Brython. It would need to contain all the credentials and passwords for your systems in order to do the healthcheck - anyone could look at the source code in the browser and see these things, it would be a security disaster. 

Actually, even if smtplib and other network tools were available in brython it would not work because of the browser security policies. It is very hard to get a script in a web page to request data from any server than the one that the page was downloaded from. So you couldn't download your script from https://abc.com and expect that it can connect to a mail server at https://xyz.com - the browser does not allow that.

So I think for these reasons that Brython may not be the tool you are looking for?

eydz zdye

unread,
Apr 8, 2021, 8:40:34 AM4/8/21
to brython
Ian,

        I see. I think this is the reason why I can' t find tutorials or docs pertaining to my needs with using Brython. I'll have to refer  to flask as you suggested.
Thank you for your help.


Cheers!

Edison
Reply all
Reply to author
Forward
0 new messages