53

I would like to create a google chrome extension. Specifically, I'd like to make a packaged app, but not a hosted app. Am I correct in thinking this limits me to JavaScript (and HTML/CSS)?

My problem is that I need to do some complex math (singular value decomposition, factor analysis) and I don't want to write algorithms for this in javascript. Python already has libraries for the functions I need (SciPy), but I can't find any indication that I can make a Chrome extension using python.

Is this correct? Do I have any other options?

boardrider
  • 5,882
  • 7
  • 49
  • 86
Jeff
  • 12,147
  • 10
  • 51
  • 87
  • 8
    SciPy is mainly a native library so cross-compilation won't help. Even if Chrome did support Python, a SciPy dependency would limit your extension's portability. – ide Feb 19 '11 at 02:51

8 Answers8

22

There is no Python built into Chrome but I am wondering whether pyjs can help you.

chx
  • 11,270
  • 7
  • 55
  • 129
17

You can make a standard Google Chrome extension with Python (server-less) https://pythonspot.com/en/create-a-chrome-plugin-with-python/

The idea is to compile Python to Javascript (technically a JS pre-compiler) using Rapydscript. Then include the generated script in the Chrome extension. The site above has a zip file with all the stuff inside.

I recommend using Rapydscript instead of Pyjamas. A Python script compiled with Rapydscript works like any other Chrome plugin.

Pyjamas scripts does not work well with Google Chrome (needs a special parameter when Chrome starts or server).

zxvn
  • 289
  • 2
  • 3
7

Although you mentioned you don't want it to be a hosted app, but this is one typical scenario where a hosted app can do.

SciPy is not a package that is easy to deploy. Even if you are writing a installed application based on SciPy, it requires some effort to deploy this dependency. A web application can help here where you put most of the hard-to-deploy dependencies on the server side (which is a one-off thing). And the client side can be really light.

KFL
  • 17,162
  • 17
  • 65
  • 89
6

You can make AJAX calls to a separate service with Python from your Google Chrome Extension. All complex logic will be moved to separate microservice. There is a video how to create Chrome extension, maybe you will find something there - https://youtu.be/l17LDVytSE8

Maksym Rudnyi
  • 443
  • 3
  • 7
2

Python Bindings for Google Chrome API might provide what you are looking for.

http://code.google.com/p/cefpython/

Surya
  • 4,824
  • 6
  • 38
  • 63
  • 2
    Those bindings are for CEF, which is explicitly not compatible with Chrome extensions, so it definitely can't be used to make them. – Jeremy Jan 13 '16 at 22:45
2

I'm writing an extension with Flexx PScript and like it very much. Here's an overview of different tools compared to PScript (do not trust this article, it's written by flexx developers:).

Winand
  • 2,093
  • 3
  • 28
  • 48
0

This is a way to achieve this using C# MVC .NET and the Python compiler. The versions are really case specific, such as the infrastructure you are making use of.

Within a controller of choice in your Web App:

one can use :

using System.Diagnostics;


ProcessStartInfo start = new ProcessStartInfo();
            Console.WriteLine("Starting Python     Engine...");
            start.FileName =     @"~\Python\Python39\python.exe";// you will need to have a copy in an accessible folder.
             start.Arguments = string.Format("{0}",     @"~/PythonScripts/PythonScriptName.py");
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            Console.WriteLine("Starting Process..." +     DateTime.Now.ToString());

Results can be retrieved as for instance:

using (Process process = Process.Start(start)) {
    using (StreamReader reader =     process.StandardOutput)
    {
        string result = reader.ReadToEnd();
        Console.Write(result);
    } } Console.WriteLine("Process Succeeded..." + DateTime.Now.ToString());

    

The results can then be populated within a model and sent to your page on Load/Post.

You can see examples of how I make use of this to evaluate z-spreads with a web front-end using python in this manner.

enter link description here

0

Yes, with py-script it's possible, see

Write Chrome Extensions in Python (Part 1)

laike9m
  • 18,344
  • 20
  • 107
  • 140