Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plots autoresize; plus other fixes #39

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src/plotly.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,20 @@ when not defined(js):
proc save*(p: SomePlot, path = "", html_template = defaultTmplString, filename = ""): string =
result = path
if result == "":
when defined(Windows):
result = getEnv("TEMP") / "x.html"
else:
result = "/tmp/x.html"
let dir = getTempDir() / "nimplotly"
createDir dir
# TODO: this unlikely to conflict with other applications but should
# implement https://github.com/brentp/nim-plotly/issues/20
# to avoid interference with multiple instances of this library
result = dir / "D20190125T182937.html"

when type(p) is Plot:
# convert traces to data suitable for plotly and fill Html template
let data_string = parseTraces(p.traces)
else:
let data_string = $p.traces
let html = html_template.fillHtmlTemplate(data_string, p, filename)

var
f: File
if not open(f, result, fmWrite):
quit "could not open file for json"
f.write(html)
f.close()
writeFile(result, html)

when not hasThreadSupport:
# some violation of DRY for the sake of better error messages at
Expand All @@ -147,9 +143,7 @@ when not defined(js):
let tmpfile = p.save(path, html_template)

showPlot(tmpfile)
sleep(1000)
## remove file after thread is finished
removeFile(tmpfile)
# todo: garbage collect `tmpfile`, see https://github.com/brentp/nim-plotly/issues/20

proc saveImage*(p: SomePlot, filename: string) =
{.fatal: "`saveImage` only supported if compiled with --threads:on!".}
Expand All @@ -171,7 +165,7 @@ when not defined(js):
if filename.len > 0:
# wait for thread to join
thr.joinThread
removeFile(tmpfile)
# todo: garbage collect `tmpfile`, see https://github.com/brentp/nim-plotly/issues/20

proc saveImage*(p: SomePlot, filename: string) =
## saves the image under the given filename
Expand Down
43 changes: 29 additions & 14 deletions src/plotly/tmpl_html.nim
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
proc removeComments(a: string): string=
## removes lines starting with `#` (convenient; avoids having to use html comments when making edits)
for a in a.splitLines:
if a.strip.startsWith "#": continue
result.add a & "\n"

const defaultTmplString = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>$title</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="plot0"></div>
<script>
Plotly.newPlot('plot0', $data, $layout)
</script>
$saveImage
</body>
<head>
<meta charset="utf-8" />
<title>$title</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="plot0"></div>
<script>
# TODO: this currently overrides size settings given in plots;
# need to expose whether to autoresize or not
# Note: this didn't seem to work: Plotly.Plots.resize('plot0');
runRelayout = function() {
var margin = 50; // if 0, would introduce scrolling
Plotly.relayout('plot0', {width: window.innerWidth - margin, height: window.innerHeight - margin } );
};
window.onresize = runRelayout;
# Consider: {responsive: true}
Plotly.newPlot('plot0', $data, $layout).then(runRelayout);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the <script> body from the defaultTmplString template, and create two different bodies. One with your JS code and one with the old one. Then the comments can just be normal Nim comments above the definition of that code and we can avoid the removeComments iteration.

Then add a field to Layout here: https://github.com/brentp/nim-plotly/blob/master/src/plotly/plotly_types.nim#L249. Maybe autoResize or something. Depending on its value either take your JS code or the old one. Then we can decide what we want to default to.

</script>
$saveImage
</body>
</html>
"""
""".removeComments

# type needs to be inserted!
# either
Expand All @@ -37,7 +52,7 @@ const injectImageCode = """
// need to wait a short while to be sure the promise is fullfilled (I believe?!)
connection.send("connected")
setTimeout(function(){ connection.send(imageData); }, 100);
};
};
</script>
"""