Generate image on-the-fly using FLask and matplotlib











up vote
0
down vote

favorite












I would like to plot an image without having to save it in a static folder.



I've got an example posted here by user Messa working just fine:



import io
import random
from flask import Response
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

@app.route('/plot.png')
def plot_png():
fig = create_figure()
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype='image/png')

def create_figure():
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
xs = range(100)
ys = [random.randint(1, 50) for x in xs]
axis.plot(xs, ys)
return fig


Then in the HTML file:



<img src="/plot.png" alt="my plot">


The result is perfect. I get the image on the fly.



Now I would like to use some parameters like this:



@app.route('/<x_values_source>/<y_values_source>/plot.png')
def plot_png(x_values_source, y_values_source):
fig = create_figure(x_values_source, y_values_source)
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype='image/png')

def create_figure(x_values_source, y_values_source):
x_values = function_that_returns_x_values_set(x_values_source)
y_values = function_that_returns_y_values_set(y_values_source)
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
axis.plot(x_values, y_values)
return fig


But of course It doesn't work. I'm quite new to Python and I'm still learing a lot of things.



Thanks!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I would like to plot an image without having to save it in a static folder.



    I've got an example posted here by user Messa working just fine:



    import io
    import random
    from flask import Response
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    @app.route('/plot.png')
    def plot_png():
    fig = create_figure()
    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')

    def create_figure():
    fig = Figure()
    axis = fig.add_subplot(1, 1, 1)
    xs = range(100)
    ys = [random.randint(1, 50) for x in xs]
    axis.plot(xs, ys)
    return fig


    Then in the HTML file:



    <img src="/plot.png" alt="my plot">


    The result is perfect. I get the image on the fly.



    Now I would like to use some parameters like this:



    @app.route('/<x_values_source>/<y_values_source>/plot.png')
    def plot_png(x_values_source, y_values_source):
    fig = create_figure(x_values_source, y_values_source)
    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')

    def create_figure(x_values_source, y_values_source):
    x_values = function_that_returns_x_values_set(x_values_source)
    y_values = function_that_returns_y_values_set(y_values_source)
    fig = Figure()
    axis = fig.add_subplot(1, 1, 1)
    axis.plot(x_values, y_values)
    return fig


    But of course It doesn't work. I'm quite new to Python and I'm still learing a lot of things.



    Thanks!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I would like to plot an image without having to save it in a static folder.



      I've got an example posted here by user Messa working just fine:



      import io
      import random
      from flask import Response
      from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
      from matplotlib.figure import Figure

      @app.route('/plot.png')
      def plot_png():
      fig = create_figure()
      output = io.BytesIO()
      FigureCanvas(fig).print_png(output)
      return Response(output.getvalue(), mimetype='image/png')

      def create_figure():
      fig = Figure()
      axis = fig.add_subplot(1, 1, 1)
      xs = range(100)
      ys = [random.randint(1, 50) for x in xs]
      axis.plot(xs, ys)
      return fig


      Then in the HTML file:



      <img src="/plot.png" alt="my plot">


      The result is perfect. I get the image on the fly.



      Now I would like to use some parameters like this:



      @app.route('/<x_values_source>/<y_values_source>/plot.png')
      def plot_png(x_values_source, y_values_source):
      fig = create_figure(x_values_source, y_values_source)
      output = io.BytesIO()
      FigureCanvas(fig).print_png(output)
      return Response(output.getvalue(), mimetype='image/png')

      def create_figure(x_values_source, y_values_source):
      x_values = function_that_returns_x_values_set(x_values_source)
      y_values = function_that_returns_y_values_set(y_values_source)
      fig = Figure()
      axis = fig.add_subplot(1, 1, 1)
      axis.plot(x_values, y_values)
      return fig


      But of course It doesn't work. I'm quite new to Python and I'm still learing a lot of things.



      Thanks!










      share|improve this question















      I would like to plot an image without having to save it in a static folder.



      I've got an example posted here by user Messa working just fine:



      import io
      import random
      from flask import Response
      from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
      from matplotlib.figure import Figure

      @app.route('/plot.png')
      def plot_png():
      fig = create_figure()
      output = io.BytesIO()
      FigureCanvas(fig).print_png(output)
      return Response(output.getvalue(), mimetype='image/png')

      def create_figure():
      fig = Figure()
      axis = fig.add_subplot(1, 1, 1)
      xs = range(100)
      ys = [random.randint(1, 50) for x in xs]
      axis.plot(xs, ys)
      return fig


      Then in the HTML file:



      <img src="/plot.png" alt="my plot">


      The result is perfect. I get the image on the fly.



      Now I would like to use some parameters like this:



      @app.route('/<x_values_source>/<y_values_source>/plot.png')
      def plot_png(x_values_source, y_values_source):
      fig = create_figure(x_values_source, y_values_source)
      output = io.BytesIO()
      FigureCanvas(fig).print_png(output)
      return Response(output.getvalue(), mimetype='image/png')

      def create_figure(x_values_source, y_values_source):
      x_values = function_that_returns_x_values_set(x_values_source)
      y_values = function_that_returns_y_values_set(y_values_source)
      fig = Figure()
      axis = fig.add_subplot(1, 1, 1)
      axis.plot(x_values, y_values)
      return fig


      But of course It doesn't work. I'm quite new to Python and I'm still learing a lot of things.



      Thanks!







      python matplotlib flask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 20:42

























      asked Nov 12 at 18:35









      Luciano

      11




      11
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          -1
          down vote













          To pass parameters in, it's best to not prefix them to the URI, since that might confuse Flask's route decoding. Instead, if you form the URL like



          /plot.png?x=1,2,3,4&y=100,200,300,400


          you can pick the args up via request.args. They'll come in as strings, so you'll need to seperate and convert to ints (or floats, if you're using real numbers)



          from flask import request

          @app.route('/plot.png')
          def plot():
          try:
          x_values = [int(x) for x in request.args.get('x','').split(',')]
          y_values = [int(y) for y in request.args.get('y','').split(',')]
          except:
          # malformed


          I have a working example here of a robust way to handle matplotlib dynamically. The general idea is



          from flask import send_file

          @app.route('/plot.png')
          def plot_png():
          fig, ax = plt.subplots()
          draw(ax)
          return fig_response(fig)

          def fig_response(fig):
          """Turn a matplotlib Figure into Flask response"""
          img_bytes = BytesIO()
          fig.savefig(img_bytes)
          img_bytes.seek(0)
          return send_file(img_bytes, mimetype='image/png')


          where you supply draw(). Take a look at my example, though, since there are some cache headers you'll want to add.






          share|improve this answer























          • Hi! Thanks for the reply. I still don't see how could I send the parameters through the route.
            – Luciano
            Nov 12 at 20:23










          • I added example code for that above.
            – Dave W. Smith
            Nov 12 at 20:45










          • Thanks! I'll give it a try. I've also modified my original question in order to make it more clear.
            – Luciano
            Nov 12 at 20:51











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268133%2fgenerate-image-on-the-fly-using-flask-and-matplotlib%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








          up vote
          -1
          down vote













          To pass parameters in, it's best to not prefix them to the URI, since that might confuse Flask's route decoding. Instead, if you form the URL like



          /plot.png?x=1,2,3,4&y=100,200,300,400


          you can pick the args up via request.args. They'll come in as strings, so you'll need to seperate and convert to ints (or floats, if you're using real numbers)



          from flask import request

          @app.route('/plot.png')
          def plot():
          try:
          x_values = [int(x) for x in request.args.get('x','').split(',')]
          y_values = [int(y) for y in request.args.get('y','').split(',')]
          except:
          # malformed


          I have a working example here of a robust way to handle matplotlib dynamically. The general idea is



          from flask import send_file

          @app.route('/plot.png')
          def plot_png():
          fig, ax = plt.subplots()
          draw(ax)
          return fig_response(fig)

          def fig_response(fig):
          """Turn a matplotlib Figure into Flask response"""
          img_bytes = BytesIO()
          fig.savefig(img_bytes)
          img_bytes.seek(0)
          return send_file(img_bytes, mimetype='image/png')


          where you supply draw(). Take a look at my example, though, since there are some cache headers you'll want to add.






          share|improve this answer























          • Hi! Thanks for the reply. I still don't see how could I send the parameters through the route.
            – Luciano
            Nov 12 at 20:23










          • I added example code for that above.
            – Dave W. Smith
            Nov 12 at 20:45










          • Thanks! I'll give it a try. I've also modified my original question in order to make it more clear.
            – Luciano
            Nov 12 at 20:51















          up vote
          -1
          down vote













          To pass parameters in, it's best to not prefix them to the URI, since that might confuse Flask's route decoding. Instead, if you form the URL like



          /plot.png?x=1,2,3,4&y=100,200,300,400


          you can pick the args up via request.args. They'll come in as strings, so you'll need to seperate and convert to ints (or floats, if you're using real numbers)



          from flask import request

          @app.route('/plot.png')
          def plot():
          try:
          x_values = [int(x) for x in request.args.get('x','').split(',')]
          y_values = [int(y) for y in request.args.get('y','').split(',')]
          except:
          # malformed


          I have a working example here of a robust way to handle matplotlib dynamically. The general idea is



          from flask import send_file

          @app.route('/plot.png')
          def plot_png():
          fig, ax = plt.subplots()
          draw(ax)
          return fig_response(fig)

          def fig_response(fig):
          """Turn a matplotlib Figure into Flask response"""
          img_bytes = BytesIO()
          fig.savefig(img_bytes)
          img_bytes.seek(0)
          return send_file(img_bytes, mimetype='image/png')


          where you supply draw(). Take a look at my example, though, since there are some cache headers you'll want to add.






          share|improve this answer























          • Hi! Thanks for the reply. I still don't see how could I send the parameters through the route.
            – Luciano
            Nov 12 at 20:23










          • I added example code for that above.
            – Dave W. Smith
            Nov 12 at 20:45










          • Thanks! I'll give it a try. I've also modified my original question in order to make it more clear.
            – Luciano
            Nov 12 at 20:51













          up vote
          -1
          down vote










          up vote
          -1
          down vote









          To pass parameters in, it's best to not prefix them to the URI, since that might confuse Flask's route decoding. Instead, if you form the URL like



          /plot.png?x=1,2,3,4&y=100,200,300,400


          you can pick the args up via request.args. They'll come in as strings, so you'll need to seperate and convert to ints (or floats, if you're using real numbers)



          from flask import request

          @app.route('/plot.png')
          def plot():
          try:
          x_values = [int(x) for x in request.args.get('x','').split(',')]
          y_values = [int(y) for y in request.args.get('y','').split(',')]
          except:
          # malformed


          I have a working example here of a robust way to handle matplotlib dynamically. The general idea is



          from flask import send_file

          @app.route('/plot.png')
          def plot_png():
          fig, ax = plt.subplots()
          draw(ax)
          return fig_response(fig)

          def fig_response(fig):
          """Turn a matplotlib Figure into Flask response"""
          img_bytes = BytesIO()
          fig.savefig(img_bytes)
          img_bytes.seek(0)
          return send_file(img_bytes, mimetype='image/png')


          where you supply draw(). Take a look at my example, though, since there are some cache headers you'll want to add.






          share|improve this answer














          To pass parameters in, it's best to not prefix them to the URI, since that might confuse Flask's route decoding. Instead, if you form the URL like



          /plot.png?x=1,2,3,4&y=100,200,300,400


          you can pick the args up via request.args. They'll come in as strings, so you'll need to seperate and convert to ints (or floats, if you're using real numbers)



          from flask import request

          @app.route('/plot.png')
          def plot():
          try:
          x_values = [int(x) for x in request.args.get('x','').split(',')]
          y_values = [int(y) for y in request.args.get('y','').split(',')]
          except:
          # malformed


          I have a working example here of a robust way to handle matplotlib dynamically. The general idea is



          from flask import send_file

          @app.route('/plot.png')
          def plot_png():
          fig, ax = plt.subplots()
          draw(ax)
          return fig_response(fig)

          def fig_response(fig):
          """Turn a matplotlib Figure into Flask response"""
          img_bytes = BytesIO()
          fig.savefig(img_bytes)
          img_bytes.seek(0)
          return send_file(img_bytes, mimetype='image/png')


          where you supply draw(). Take a look at my example, though, since there are some cache headers you'll want to add.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 20:43

























          answered Nov 12 at 19:06









          Dave W. Smith

          16k22430




          16k22430












          • Hi! Thanks for the reply. I still don't see how could I send the parameters through the route.
            – Luciano
            Nov 12 at 20:23










          • I added example code for that above.
            – Dave W. Smith
            Nov 12 at 20:45










          • Thanks! I'll give it a try. I've also modified my original question in order to make it more clear.
            – Luciano
            Nov 12 at 20:51


















          • Hi! Thanks for the reply. I still don't see how could I send the parameters through the route.
            – Luciano
            Nov 12 at 20:23










          • I added example code for that above.
            – Dave W. Smith
            Nov 12 at 20:45










          • Thanks! I'll give it a try. I've also modified my original question in order to make it more clear.
            – Luciano
            Nov 12 at 20:51
















          Hi! Thanks for the reply. I still don't see how could I send the parameters through the route.
          – Luciano
          Nov 12 at 20:23




          Hi! Thanks for the reply. I still don't see how could I send the parameters through the route.
          – Luciano
          Nov 12 at 20:23












          I added example code for that above.
          – Dave W. Smith
          Nov 12 at 20:45




          I added example code for that above.
          – Dave W. Smith
          Nov 12 at 20:45












          Thanks! I'll give it a try. I've also modified my original question in order to make it more clear.
          – Luciano
          Nov 12 at 20:51




          Thanks! I'll give it a try. I've also modified my original question in order to make it more clear.
          – Luciano
          Nov 12 at 20:51


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268133%2fgenerate-image-on-the-fly-using-flask-and-matplotlib%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)