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
innative_host.py
Simply trying to import the code fails with the effect that the native host doesn't respond. The extension error readsUnchecked 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")
innative_host.py
My local terminal gave this string result forwhich pipenv
, so I tried that in the native host. Same result.subprocess.call("flask pdf combine")
Same result.import click
innative_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 functionsome_script.do()
innative_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 wheresome_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")
, oros.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.
On the terminal, go into pipenv shell
Copy the path that is output from which python3
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
innative_host.py
Simply trying to import the code fails with the effect that the native host doesn't respond. The extension error readsUnchecked 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")
innative_host.py
My local terminal gave this string result forwhich pipenv
, so I tried that in the native host. Same result.subprocess.call("flask pdf combine")
Same result.import click
innative_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 functionsome_script.do()
innative_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 wheresome_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")
, oros.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.
On the terminal, go into pipenv shell
Copy the path that is output from which python3
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?
1 Answer
Reset to default 1Is 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.
On the terminal, go into
pipenv shell
Copy the path that is output from
which python3
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 ofos.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 Run - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744687903a2619815.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论