Python Forum

Full Version: Is it possible to import / run compiled Python (.pyd) file in web browser?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am new to Python and am trying to import a compiled Python library file (.pyd) into a Python script in a web browser. I have been trying to do this with Brython but have found that it is not possible in that environment. Brython cannot parse / execute compiled Python.

My problem is that I don't know if it's just Brython that this can't be done in or if it is just not possible in a web browser at all. I can find references to how the generic Python include will look for .py, .pyc and then .pyd extensions, but I cannot find anything online that deals with how to do it in a web browser, or indeed if it can even be done in a web browser. This leads me to suspect maybe it is not possible?

Please could someone let me know - is it possible to include a compiled python (.pyd) file into a web-browser Python script? If it is, please could someone point me in the right direction? I am not looking for a solution or finished code or anything - I'm happy to do tons of research and coding - I just need help getting started as I've drawn blanks so far.

Thanks in advance.
From my understanding, a pyd file is basically a windows DLL. It doesn't make sense to me to try to force a DLL to work in a browser - I use OS X at work, Linux at home, and Android on mobile, so what you're doing wouldn't be accessible at all to someone like me.
Hi,

It's a compiled file which contains functionality that the browser app needs. The user base is exclusively running Windows PCs, so there won't be the case of someone on OS-X, Android or whatever trying to use the browser app.

I'd agree that in most circumstances it doesn't seem like a sensible thing to want to do, but in this instance it is a valid requirement - honest! Smile

Aside form wisdom or otherwise of doing it, can it actually be done?

Thanks!
dvid Wrote:Aside form wisdom or otherwise of doing it, can it actually be done?
They more normal way is to have python code on server side,
then send or call(using eg jinjaAjax) that code on client side.

I have a tutorial about this here,
so the point is to throw away the JavaScript code(that can run in Browser)
and do the same with Python code.
Doing a Ajax call to Python function server side,
so for the user in browser there is no diffidence as JavaScript and Python is doing the same task here.
In order for your code to show, you need several things:

Firstly, there needs to be a server that handles HTTP requests. At the moment you are just opening a file with Firefox on your local hard drive. A server like Apache or something similar is required.

Secondly, presuming that you now have a server that serves the files, you will also need something that interprets the code as Python code for the server. For Python users, the go-to solution is nowadays mod_wsgi. But for simpler cases, you could stick with CGI (more info here), but if you want to produce web pages easily, you should go with an existing Python web framework like Django.

Setting this up can be quite the hassle, so be prepared.