From flask, how to send the matplotlib plot image into JSON object?
We are trying to create an API that works with React on the frontend and Flask on the backend. As a part of this, the output that needs to be displayed includes four entities:
2 images (generated using matplotlib)
2 strings.
I have embedded both strings into a JSON object but the images are proving to be difficult.
Is there a way to not store these images and directly embed the generated matplotlib images into the JSON object? Most methods require us to specify the image file name but is there a way to skip saving the files? The sendfile() wouldn't work because we have multiple outputs and our final objective is the JSON response.
Tried creating a temporary file and reading it and this is generating a Buffered Streams ? Base64 would not apply on this I think?
Thank you in advance for the help.
python json image matplotlib flask
add a comment |
We are trying to create an API that works with React on the frontend and Flask on the backend. As a part of this, the output that needs to be displayed includes four entities:
2 images (generated using matplotlib)
2 strings.
I have embedded both strings into a JSON object but the images are proving to be difficult.
Is there a way to not store these images and directly embed the generated matplotlib images into the JSON object? Most methods require us to specify the image file name but is there a way to skip saving the files? The sendfile() wouldn't work because we have multiple outputs and our final objective is the JSON response.
Tried creating a temporary file and reading it and this is generating a Buffered Streams ? Base64 would not apply on this I think?
Thank you in advance for the help.
python json image matplotlib flask
add a comment |
We are trying to create an API that works with React on the frontend and Flask on the backend. As a part of this, the output that needs to be displayed includes four entities:
2 images (generated using matplotlib)
2 strings.
I have embedded both strings into a JSON object but the images are proving to be difficult.
Is there a way to not store these images and directly embed the generated matplotlib images into the JSON object? Most methods require us to specify the image file name but is there a way to skip saving the files? The sendfile() wouldn't work because we have multiple outputs and our final objective is the JSON response.
Tried creating a temporary file and reading it and this is generating a Buffered Streams ? Base64 would not apply on this I think?
Thank you in advance for the help.
python json image matplotlib flask
We are trying to create an API that works with React on the frontend and Flask on the backend. As a part of this, the output that needs to be displayed includes four entities:
2 images (generated using matplotlib)
2 strings.
I have embedded both strings into a JSON object but the images are proving to be difficult.
Is there a way to not store these images and directly embed the generated matplotlib images into the JSON object? Most methods require us to specify the image file name but is there a way to skip saving the files? The sendfile() wouldn't work because we have multiple outputs and our final objective is the JSON response.
Tried creating a temporary file and reading it and this is generating a Buffered Streams ? Base64 would not apply on this I think?
Thank you in advance for the help.
python json image matplotlib flask
python json image matplotlib flask
edited Nov 20 at 11:43
asked Nov 20 at 7:39
smitha
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Assuming you have your plot looking the way you'd like, you can always base64 encode the file it's stored in and reproduce it on another machine. Take a look:
# assume your plot is saved to tfile
with open(tfile) as fin:
read_in = fin.read()
b64_data = base64.urlsafe_b64encode(read_in.decode('utf-8'))
# now send over the network
If we don't store the image generated then what would we pass as an argument into the open(), can we just use a variable object? Shouldn't it always be a file name (which we don't have here, in our case).
– smitha
Nov 20 at 13:18
You would have to save it to a file, but it could be a tempfile, which is deleted after the run.
– hd1
Nov 21 at 0:29
The temp file is sort of giving a bufferedstream which is not readable directly with the f.read() method, we've tried using the io.BufferedReader but it shows an empty stream.
– smitha
Nov 21 at 4:52
Yes, the tempfile has a .name parameter returning its name.
– hd1
Nov 21 at 8:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388276%2ffrom-flask-how-to-send-the-matplotlib-plot-image-into-json-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming you have your plot looking the way you'd like, you can always base64 encode the file it's stored in and reproduce it on another machine. Take a look:
# assume your plot is saved to tfile
with open(tfile) as fin:
read_in = fin.read()
b64_data = base64.urlsafe_b64encode(read_in.decode('utf-8'))
# now send over the network
If we don't store the image generated then what would we pass as an argument into the open(), can we just use a variable object? Shouldn't it always be a file name (which we don't have here, in our case).
– smitha
Nov 20 at 13:18
You would have to save it to a file, but it could be a tempfile, which is deleted after the run.
– hd1
Nov 21 at 0:29
The temp file is sort of giving a bufferedstream which is not readable directly with the f.read() method, we've tried using the io.BufferedReader but it shows an empty stream.
– smitha
Nov 21 at 4:52
Yes, the tempfile has a .name parameter returning its name.
– hd1
Nov 21 at 8:06
add a comment |
Assuming you have your plot looking the way you'd like, you can always base64 encode the file it's stored in and reproduce it on another machine. Take a look:
# assume your plot is saved to tfile
with open(tfile) as fin:
read_in = fin.read()
b64_data = base64.urlsafe_b64encode(read_in.decode('utf-8'))
# now send over the network
If we don't store the image generated then what would we pass as an argument into the open(), can we just use a variable object? Shouldn't it always be a file name (which we don't have here, in our case).
– smitha
Nov 20 at 13:18
You would have to save it to a file, but it could be a tempfile, which is deleted after the run.
– hd1
Nov 21 at 0:29
The temp file is sort of giving a bufferedstream which is not readable directly with the f.read() method, we've tried using the io.BufferedReader but it shows an empty stream.
– smitha
Nov 21 at 4:52
Yes, the tempfile has a .name parameter returning its name.
– hd1
Nov 21 at 8:06
add a comment |
Assuming you have your plot looking the way you'd like, you can always base64 encode the file it's stored in and reproduce it on another machine. Take a look:
# assume your plot is saved to tfile
with open(tfile) as fin:
read_in = fin.read()
b64_data = base64.urlsafe_b64encode(read_in.decode('utf-8'))
# now send over the network
Assuming you have your plot looking the way you'd like, you can always base64 encode the file it's stored in and reproduce it on another machine. Take a look:
# assume your plot is saved to tfile
with open(tfile) as fin:
read_in = fin.read()
b64_data = base64.urlsafe_b64encode(read_in.decode('utf-8'))
# now send over the network
answered Nov 20 at 11:51
hd1
24.2k35467
24.2k35467
If we don't store the image generated then what would we pass as an argument into the open(), can we just use a variable object? Shouldn't it always be a file name (which we don't have here, in our case).
– smitha
Nov 20 at 13:18
You would have to save it to a file, but it could be a tempfile, which is deleted after the run.
– hd1
Nov 21 at 0:29
The temp file is sort of giving a bufferedstream which is not readable directly with the f.read() method, we've tried using the io.BufferedReader but it shows an empty stream.
– smitha
Nov 21 at 4:52
Yes, the tempfile has a .name parameter returning its name.
– hd1
Nov 21 at 8:06
add a comment |
If we don't store the image generated then what would we pass as an argument into the open(), can we just use a variable object? Shouldn't it always be a file name (which we don't have here, in our case).
– smitha
Nov 20 at 13:18
You would have to save it to a file, but it could be a tempfile, which is deleted after the run.
– hd1
Nov 21 at 0:29
The temp file is sort of giving a bufferedstream which is not readable directly with the f.read() method, we've tried using the io.BufferedReader but it shows an empty stream.
– smitha
Nov 21 at 4:52
Yes, the tempfile has a .name parameter returning its name.
– hd1
Nov 21 at 8:06
If we don't store the image generated then what would we pass as an argument into the open(), can we just use a variable object? Shouldn't it always be a file name (which we don't have here, in our case).
– smitha
Nov 20 at 13:18
If we don't store the image generated then what would we pass as an argument into the open(), can we just use a variable object? Shouldn't it always be a file name (which we don't have here, in our case).
– smitha
Nov 20 at 13:18
You would have to save it to a file, but it could be a tempfile, which is deleted after the run.
– hd1
Nov 21 at 0:29
You would have to save it to a file, but it could be a tempfile, which is deleted after the run.
– hd1
Nov 21 at 0:29
The temp file is sort of giving a bufferedstream which is not readable directly with the f.read() method, we've tried using the io.BufferedReader but it shows an empty stream.
– smitha
Nov 21 at 4:52
The temp file is sort of giving a bufferedstream which is not readable directly with the f.read() method, we've tried using the io.BufferedReader but it shows an empty stream.
– smitha
Nov 21 at 4:52
Yes, the tempfile has a .name parameter returning its name.
– hd1
Nov 21 at 8:06
Yes, the tempfile has a .name parameter returning its name.
– hd1
Nov 21 at 8:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388276%2ffrom-flask-how-to-send-the-matplotlib-plot-image-into-json-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown