admin管理员组

文章数量:1389841

I'm trying to use a flask app cli tool to process some data for a chrome extension. I was able to set up the native messaging host and establish a connection that successfully receives a request and responds to it. But I'm having trouble triggering the cli tool.

The reason I want a CLI tool as a backend is so that it can function as both a standalone CLI app and process various chrome extension tasks.

It seems the main issue is that pipenv and python and python3 aren't recognized by the system when it's invoked from the extension. Is it possible to set up a chrome extension native host that 1) is able to use the pipenv shell or 2) can access a flask app and its cli commands? I'm wondering if a server might be necessary somehow, but it seems like starting/using it on the local system (as opposed to hosting it remotely) might be out of the question.

Project structure:

\root
    \app
        \commands
            \pdf_group
            __init__.py
            combine.py
        __init__.py
    __init__.py
    app.py
Pipfile
native_host.py
some_script.py

What I've tried so far:

  • from appmands import my_cli_command in native_host.py Simply trying to import the code fails with the effect that the native host doesn't respond. The extension error reads Unchecked runtime.lastError: Error when communicating with the native messaging host. Same for the full import path, from appmands.pdf_groupbine import my_cli_command.

  • os.system("/opt/homebrew/bin/pipenv") in native_host.py My local terminal gave this string result for which pipenv, so I tried that in the native host. Same result.

  • subprocess.call("flask pdf combine") Same result.

  • import click in native_host.py Same result. Click is part of flask, so I think the third-party nature somehow contributes to the failure.

  • import some_script, run script function some_script.do() in native_host.py Same result. This script is in the root level of the project. (do() is obviously not a proper name for a script main function, I am hoping for a solution where some_script is unnecessary.)

Basically, any third party code from the app or the cli app itself doesn't work in native_host.py and leads the native host to fail to respond.

  • os.system("which python"), or os.system("which pipenv") The output result was error 256, indicating that the system can't find the path for these.

  • I tried all of these methods from the command line/in a python shell. They all worked as expected there.

EDIT

I don't think this is the ideal approach, but I was able to get this to work.

  1. On the terminal, go into pipenv shell

  2. Copy the path that is output from which python3

  3. In native_host.py, include the outputted path to run the app.

import os
os.system("/Users/bernadette/.local/share/virtualenvs/convert_and_combine_pdfs-mqF7aRtH/bin/python3 ./some_script.py")

What seemed to make the difference was externally(?) accessing the virtual environment. But it doesn't seem to handle sending the native host's response back properly.

Remaining questions

  • Is a script within a script the only way to get this working?

  • subprocess.call("...") didn't seem to work, is there a way to use that instead of os.system?

I'm trying to use a flask app cli tool to process some data for a chrome extension. I was able to set up the native messaging host and establish a connection that successfully receives a request and responds to it. But I'm having trouble triggering the cli tool.

The reason I want a CLI tool as a backend is so that it can function as both a standalone CLI app and process various chrome extension tasks.

It seems the main issue is that pipenv and python and python3 aren't recognized by the system when it's invoked from the extension. Is it possible to set up a chrome extension native host that 1) is able to use the pipenv shell or 2) can access a flask app and its cli commands? I'm wondering if a server might be necessary somehow, but it seems like starting/using it on the local system (as opposed to hosting it remotely) might be out of the question.

Project structure:

\root
    \app
        \commands
            \pdf_group
            __init__.py
            combine.py
        __init__.py
    __init__.py
    app.py
Pipfile
native_host.py
some_script.py

What I've tried so far:

  • from appmands import my_cli_command in native_host.py Simply trying to import the code fails with the effect that the native host doesn't respond. The extension error reads Unchecked runtime.lastError: Error when communicating with the native messaging host. Same for the full import path, from appmands.pdf_groupbine import my_cli_command.

  • os.system("/opt/homebrew/bin/pipenv") in native_host.py My local terminal gave this string result for which pipenv, so I tried that in the native host. Same result.

  • subprocess.call("flask pdf combine") Same result.

  • import click in native_host.py Same result. Click is part of flask, so I think the third-party nature somehow contributes to the failure.

  • import some_script, run script function some_script.do() in native_host.py Same result. This script is in the root level of the project. (do() is obviously not a proper name for a script main function, I am hoping for a solution where some_script is unnecessary.)

Basically, any third party code from the app or the cli app itself doesn't work in native_host.py and leads the native host to fail to respond.

  • os.system("which python"), or os.system("which pipenv") The output result was error 256, indicating that the system can't find the path for these.

  • I tried all of these methods from the command line/in a python shell. They all worked as expected there.

EDIT

I don't think this is the ideal approach, but I was able to get this to work.

  1. On the terminal, go into pipenv shell

  2. Copy the path that is output from which python3

  3. In native_host.py, include the outputted path to run the app.

import os
os.system("/Users/bernadette/.local/share/virtualenvs/convert_and_combine_pdfs-mqF7aRtH/bin/python3 ./some_script.py")

What seemed to make the difference was externally(?) accessing the virtual environment. But it doesn't seem to handle sending the native host's response back properly.

Remaining questions

  • Is a script within a script the only way to get this working?

  • subprocess.call("...") didn't seem to work, is there a way to use that instead of os.system?

Share Improve this question edited Mar 27 at 13:15 Boom100100 asked Mar 13 at 17:26 Boom100100Boom100100 1378 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Is it possible to set up a chrome extension native host that 1) is able to use the pipenv shell or 2) can access a flask app and its cli commands?

Option 2 is possible and all that's necessary.

I don't think this is the ideal approach, but I was able to get this to work.

  1. On the terminal, go into pipenv shell

  2. Copy the path that is output from which python3

  3. In native_host.py, include the following to run the app.

import os
os.system("/Users/bernadette/.local/share/virtualenvs/convert_and_combine_pdfs-mqF7aRtH/bin/python3 ./some_script.py")

What seemed to make the difference was externally(?) accessing the virtual environment. But it doesn't seem to handle sending the native host's response back properly.

Setting .venv in the root of the project and running pipenv shell allowed me to create a relative path to the necessary version of python.

  • Is a script within a script the only way to get this working?

It seems so. The third party and app modules were only able to import from some_script.py. But, the native_host.py script calling another script is probably ideal for stronger encapsulation. The CLI tool can now be used as a native host for multiple Chrome extensions instead of just the one.

  • subprocess.call("...") didn't seem to work, is there a way to use that instead of os.system?

I used subprocess.Popen instead. Setting stdout handled issues related to print interfering with the process.

The final code:

process = subprocess.Popen(
        [
            ".venv/bin/python3",
            "./some_script.py",
            json.dumps(receivedMessage),
        ],
        stdout=subprocess.PIPE
    )
    stdout, stderr = processmunicate()

本文标签: Chrome Native Messaging Host in Python Pipenv Not Recognized and Flask App Fails to RunStack Overflow