我有一个执行octave-cli的docker容器,我想要的是将该可执行文件作为环境变量放在另一个容器中,以便可以使用python脚本运行它。
我正在python脚本中使用oct2py库,该库需要具有八度可执行文件的环境变量。没有要求使用最新版本八度的PPA,因此我发现了一个八度docker映像(mtmiller / octave)与我的.m文件配合使用。现在,我希望它在我的python容器中可用,以便脚本可以工作。
function hello()
printf("hello,world!")
endfunction
from oct2py import octave
octave.add('path/to/octavefile')
octave.hello()
hello,world!
在python脚本上运行的要求
您必须在PATH环境变量中安装GNU Octave。另外,您可以设置一个指向octave-cli可执行文件本身的OCTAVE_EXECUTABLE或OCTAVE环境变量。
现在,我有mtmiller / octave执行八度。如何在python容器的PATH中设置它?
我怀疑八度提供了一个Web api软件包……。但是我对此一无所知,但是您可以在八度服务器上运行轻量级的api服务器。
octave-server.py
import flask
import subprocess
from oct2py import octave
octave.add('path/to/octavefile')
app = flask.Flask(__name__)
@app.route("hello.m")
def hello_fn_call():
users_name = flask.request.args.get("name")
return json.dumps(octave.hello(users_name))
if __name__ == "__main__":
# you should really serve this with nginx + gunicorn or something
app.run(debug=True)
octave_rest.py
import requests
def say_hello(username):
return requests.get("http://other-image:5000/hello.m",{"name":username}).json()
if __name__ == "__main__":
print(say_hello("BOB"))
# something like "Hello, Bob"
如果您真的想在倍频程程序中打印字符串,则需要捕获标准,这个装饰器应该有所帮助(粗略地讲,您也可以手动完成所有操作)
from contextlib import redirect_stdout # py3.4+ only
import io
...
@app.route("hello.m")
def hello_fn_call():
users_name = flask.request.args.get("name")
f = io.StringIO()
with redirect_stdout(f):
hello(users_name) # capture stdout
return json.dumps(f.getvalue())