-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7beaf0
commit c69f4ef
Showing
3 changed files
with
132 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,89 @@ | ||
import * as React from 'react' | ||
import JSONTree from 'react-json-tree'; | ||
import { executePython, readAllExamplesPythonCode } from './python' | ||
|
||
const Component = (props: {data: any}) => { | ||
|
||
const { data } = props | ||
const parsedData = JSON.parse(data) | ||
|
||
return ( | ||
<div style={{overflow: 'auto'}}> | ||
<JSONTree | ||
data={parsedData} | ||
/> | ||
</div> | ||
) | ||
|
||
export type TFRecordProps = { | ||
} | ||
export type TFRecordState = { | ||
path: string | ||
data: any | ||
} | ||
export class TFRecordViewerComponent extends React.Component<TFRecordProps, TFRecordState> { | ||
|
||
constructor(props: TFRecordProps) { | ||
super(props) | ||
this.state = { | ||
path: '/home/max/projects/jupyterlab_tfrecord_viewer/sample_record.record', | ||
data: null, | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
executePython(readAllExamplesPythonCode, | ||
res => { | ||
// want to clean out all the binary | ||
const cleaned = res.replace(/b\'(.|\n)*\'/g, '{binary...}') | ||
|
||
this.setState({data: cleaned}) | ||
} | ||
) | ||
} | ||
|
||
onRequestLoad = () => { | ||
console.log('onRequestLoad') | ||
} | ||
|
||
handlePathChange = (event: any) => { | ||
console.log('handlePathChange') | ||
this.setState({path: event.target.value}) | ||
} | ||
|
||
render() { | ||
|
||
const { data } = this.state | ||
const parsedData = JSON.parse(data) | ||
|
||
return ( | ||
<div> | ||
|
||
<hr/> | ||
<input | ||
style={{width: '50%'}} | ||
type="text" | ||
value={this.state.path} | ||
onChange={this.handlePathChange} /> | ||
|
||
<button | ||
onClick={this.onRequestLoad}> | ||
WHELP | ||
</button> | ||
|
||
<br/> | ||
|
||
{ | ||
this.state.data | ||
? <JSONTree | ||
data={parsedData} | ||
/> | ||
: 'Loading data...' | ||
} | ||
|
||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default TFRecordViewerComponent | ||
|
||
|
||
// let textInput = document.createElement('input') | ||
// textInput.value = '/home/max/projects/jupyterlab_tfrecord_viewer/sample_record.record' | ||
// textInput.style.cssText = 'width: calc(100% - 70px);' | ||
// widget.node.appendChild(textInput) | ||
|
||
export default Component | ||
// let button = document.createElement('button') | ||
// button.style.cssText = 'width: 60px;' | ||
// button.innerHTML = 'Load' | ||
// button.onclick = _mouseEvent => console.log('button clicked', textInput.value) | ||
// widget.node.appendChild(button) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Kernel } from '@jupyterlab/services' | ||
|
||
|
||
export async function executePython(code: string, onResult: (s: string) => any) { | ||
const kernelSpecs = await Kernel.getSpecs() | ||
const kernel = await Kernel.startNew({ | ||
name: kernelSpecs.default | ||
}) | ||
const execution = kernel.requestExecute({code}) | ||
|
||
execution.done.then(() => { | ||
}) | ||
execution.onIOPub = msg => { | ||
console.log('msg content', msg.content) | ||
|
||
if (msg.content.name === 'stdout') | ||
onResult(msg.content.text as string) | ||
} | ||
} | ||
|
||
|
||
export const readAllExamplesPythonCode = ` | ||
import tensorflow as tf | ||
from google.protobuf.json_format import MessageToJson | ||
try: | ||
record_path = '/home/max/projects/jupyterlab_tfrecord_viewer/sample_record.record' | ||
record_iterator = tf.python_io.tf_record_iterator(path=record_path) | ||
all_features = [] | ||
for str_record in record_iterator: | ||
example = tf.train.Example() | ||
example.ParseFromString(str_record) | ||
# features = str(example.features) | ||
features = MessageToJson(example.features) | ||
all_features.append(features) | ||
print('[') | ||
print(",\\n".join(all_features)) | ||
print(']') | ||
except: | ||
print('Failed to read record') | ||
` |