How to work with python as client side scripting language - python

I have a fully functional gaze tracking code Python project which has been tested directly through the terminal. My guide wants me to implement this over the web. He wants all the processing to be done on the client-side because our servers are not powerful enough to handle multiple requests at the same time. My code involves the use of libraries like OpenCV, DLIB and mediapipe. I wanted to know whether is it possible to implement the code on the client-side by using python itself.
And project contains import statements as
import cv2
import dlib
from enum import Enum
from threading import Timer
import pyautogui
Even if I switch to JavaScript for client-side scripting will it result in performance losses, is there any way to avoid them? Is there any other way to implement the code on the client-side by keeping everything in python?

Not usually a done thing, JS is the only really supported browser-client-side language, although you can now also use WASM.
There are a few JS tools to allow Python running in the browser, most popularly Brython and Skulpt.
However, they may not work with all your imported packages, and will be much slower than writing your code in JS.
It's worth giving those two a go, but unfortunately, I think you may have to move to JS, or to wrapping your code in a web app framework (eg django) to allow the processing to happen on the server, where it has access to those libraries.

Related

What is the recommended practice in django to execute external scripts?

I'm planning to build a WebApp that will need to execute scripts based on the argument that an user will provide in a text-field or in the Url.
possible solutions that I have found:
create a lib directory in the root directory of the project, and put the scripts there, and import it from views.
using subprocess module to directly run the scripts in the following way:
subprocess.call(['python', 'somescript.py', argument_1,...])
argument_1: should be what an end user provides.
I'm planning to build a WebApp that will need to execute scripts
Why should it "execute scripts" ? Turn your "scripts" into proper modules, import the relevant functions and call them. The fact that Python can be used as a "scripting language" doesn't mean it's not a proper programming language.
Approach (1) should be the default approach. Never subprocess unless you absolutely have to.
Disadvantages of subprocessing:
Depends on the underlying OS and in your case Python (i.e. is python command the same as the Python that runs the original script?).
Potentially harder to make safe.
Harder to pass values, return results and report errors.
Eats more memory and cpu (a side effect is that you can utilize all cpu cores but since you are writing a web app it is likely you do that anyway).
Generally harder to code and maintain.
Advantages of subprocessing:
Isolates the runtime. This is useful if for example scripts are uploaded by users. You don't want them to mess with your application.
Related to 1: potentially easier to dynamically add scripts. Not that you should do that anyway. Also becomes harder when you have more then 1 server and you need to synchronize them.
Well, you can run non-python code that way. But it doesn't apply to your case.

Using HTML to implement Python User Interface

I've just started learning a bit of Python and I'm currently trying to implement a Python UI through HTML. Is there anything in vanilla Python that would allow for this, similar to how you can create UI's with Java and XML with JFX or will I have to use a framework such as Django?
I'm reluctant to use Django as there are many features that I do not need
Thanks,
Michael
In vanilla python wsgiref is very helpful for building the server-side of web-applications (possibly with str.format or string.Template and/or json) but if you want it more direct communication I would suggest XML-RPC (there are good js-clients out there).
It is also possible to execute Python Scripts right in your Website with Brython or (with strong constraints) Ironpython
For Windows you can build HTML Applications with Ironpython (have not tried but in theory it should work) or Brython (if you dont want to require the user to have ironpython installed)
You can also use Pyjs to build applications but while it uses html and javascript, i think it you dont see much of it.
There are HTML things in some ui-libraries like in wxpython (I am quite sure you will find them in many libraries)

Running Python Script in HTML

I'm working on a School Project. I've done a lot of Python Script before and I was wondering if I could like import python in html like javascript? How should I do it? Example is importing time. I want to show a Time clock in my webpage from python script.
There are python implementation written in JS. However you cannot run "natively" python in browser, you can actually run python code via javascript or compile python code to js (llvm emscripten)
You are looking for:
skulpt - http://www.skulpt.org/
brython - http://www.brython.info/
pyjs - http://pyjs.org/
pyjamas
And IMHO the most worth to take a look pypy.js:
PyPy.js is an experiment in building a fast and compliant python
environment for the web.
It uses the PyPy python interpreter, compiled for the web via
emscripten, with a custom JIT backend that emits asm.js code at
runtime.
Drawbacks of python in browser
performance (I found that only pypy.js provides benchmarks) is N-time worse
js interpreter libs (needed to load/run python code), have significant size couple MB
These two are the case, why that projects, probably shouldn't be used in the production, at that stage if ever. There are more cons like compatibility, implementation completion...
I is not possible to import Python code in html as you import JavaScript code. JavaScript is executed by the browser of the client and the browsers don't have an included Python interpreter. You have to do it with JavaScript if you want to do it on the Client side.
Here's (I think) the closest you're going to get to doing that. You can use Django and some of its functionalities:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Cython development workflow?

I'm quite new to Cython, and I come from a web development background. I'm wondering if there's is a workflow for Cython development that's similar to workflows I'm familiar with from web development.
For example, when using CoffeeScript to develop a Node.js web app, I have a Cakefile that watches for changes in the CoffeeScript source and automatically compiles the source to Javascript every time a .coffee file is written.
What is the proper way to accomplish something similar in a Cython development environment?
First of all, from my experience this kind of workflow is not particularly customary outside web development. However, there are a couple of options that come at least close to what you ask for.
You may want to use some sort of automated build tool like SCons. The Cython developers even provide a build tool for it.
Then there is setuptools/distutils, which are mostly for packaging Python software but also support building C extensions. I think, this is even the canonical way of building Cython source into shared objects.
There is another alternative also part of Cython itself: using pyximport you can simply import Cython modules like so:
import pyximport; pyximport.install()
import foo
You don't have to worry about compilation then, since pyximport automatically compiles the module on import if necessary.
I hope one of these options at least resembles what you were looking for ;)

How to export python functions and reuse them in another app

I was wondering if there is any way to export python functions to dll. There is py2exe and I can successfully create exe file. My program should be used by another program written in delphi (there is possibility of importing dll's in delphi).
So I was wondering what would be the best way to connect those 2 applications.
Now I can only create exe, execute process in delphi and communicate in some way. But I don't think that's nice way. Maybe somebody have any experience in this subject?
There are some pretty big challenges to making languages work well together. As a simple alternative to trying to hook python code directly into delphi, you could consider using something like an xmlrpc server to provide python functionality remotely.
http://docs.python.org/library/xmlrpclib.html
Of course, any protocol could be used; xmlrpc just has some useful server utilities in python and presumably has a client library in delphi.
You can re-use python functions via modules. Integrating with other language is a different task all together.
py2exe packs all dependent modules and additional dlls required by an application so that it can be easily distributed without creating any installation dependencies for the user.
Cross - language integration requires some work. To integrate with "C", there are various ways like cython etc. If there is similar facility available with delphi, you might be able to use it.
Check out some of these references, it will make it more clear to you on what direction to take.
http://wiki.python.org/moin/IntegratingPythonWithOtherLanguages
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=python+delphi (Google search)

Categories

Resources