Skip to content

Commit

Permalink
Add stdout capture
Browse files Browse the repository at this point in the history
  • Loading branch information
floryst committed Feb 3, 2020
1 parent 3ea9367 commit 3c1a371
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from helper import Api, rpc
from helper import Api, rpc, forward_stdout

class AlgorithmApi(Api):
def __init__(self):
Expand Down Expand Up @@ -48,6 +48,7 @@ def params(self):
]

@rpc('run')
@forward_stdout
def run(self, params):
input_image = params['input_image']
labelmap = params['input_labelmap']
Expand Down
37 changes: 37 additions & 0 deletions server/helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import weakref
import uuid

Expand All @@ -8,6 +9,42 @@
from serializable import serialize, unserialize
import transformers # register our serializers/unserializers

class WsOut(object):
def __init__(self, protocol):
self.protocol = protocol

def write(self, b):
self.protocol.publish('streams.stdout', b)

def flush(self):
pass

class NetworkTee(object):
def __init__(self, netout):
self.netout = netout
self.stdout = sys.stdout

def write(self, b):
self.netout.write(b)
self.stdout.write(b)

def flush(self):
self.netout.flush()
self.stdout.flush()


def forward_stdout(fn):
def handler(self, *args, **kwargs):
wsout = WsOut(self)
tee = NetworkTee(wsout)
sys.stdout = tee

ret = fn(self, *args, **kwargs)

sys.stdout = tee.stdout
return ret
return handler

def rpc(name):
def wrapper(fn):
def handler(self, *args, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions src/components/tools/RemoteProcessing/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
computed: {
...mapState('remote', {
processing: 'processing',
serverStdout: 'serverStdout',
parameters: (state) => state.paramOrder.map((name) => state.params[name]),
}),
},
Expand Down Expand Up @@ -61,6 +62,7 @@ export default {
...mapActions('remote', {
fetchParamList: 'fetchParamList',
runRemoteAlgorithm: 'runRemoteAlgorithm',
clearOutput: 'clearStdout',
setParameter: (dispatch, name, value) => {
dispatch('setParameter', { name, value });
},
Expand Down
7 changes: 7 additions & 0 deletions src/components/tools/RemoteProcessing/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@
.container * {
user-select: none;
}

.serverOutput {
width: 100%;
white-space: nowrap;
border-radius: 3px;
border: 1px solid grey;
}
9 changes: 9 additions & 0 deletions src/components/tools/RemoteProcessing/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@
{{ error }}
</v-alert>
</v-layout>
<v-layout wrap align-center class="mt-3">
<v-flex xs10><span class="caption">Server output</span></v-flex>
<v-flex xs2>
<v-btn icon @click="clearOutput"><v-icon>mdi-delete</v-icon></v-btn>
</v-flex>
<v-flex xs12>
<textarea :class="$style.serverOutput">{{ serverStdout }}</textarea>
</v-flex>
</v-layout>
</v-container>
15 changes: 14 additions & 1 deletion src/store/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default ({ proxyManager, remote }) => ({
processing: false,
params: {},
paramOrder: [],
serverStdout: '',
}),

mutations: {
Expand All @@ -39,13 +40,24 @@ export default ({ proxyManager, remote }) => ({
processing(state, flag) {
state.processing = flag;
},
appendStdout(state, text) {
state.serverStdout += text;
},
clearStdout(state) {
state.serverStdout = '';
},
},

actions: {
connect({ commit }, endpoint) {
return remote
.connect(endpoint)
.then(() => commit('connected'))
.then(() => {
commit('connected');
remote.session.subscribe('streams.stdout', (stdout) =>
commit('appendStdout', stdout)
);
})
.catch((error) => commit('connectError', error));
},
fetchParamList({ commit }) {
Expand Down Expand Up @@ -80,5 +92,6 @@ export default ({ proxyManager, remote }) => ({
return promise;
},
setParameter: wrapMutationAsAction('setParameter'),
clearStdout: wrapMutationAsAction('clearStdout'),
},
});

0 comments on commit 3c1a371

Please sign in to comment.