简体   繁体   中英

How to display python application in html?

I have written a program in Python that draws some graphs using matplotlib and tkinter .
I would like to display that program on my web page for free usage.
How can I get this Python program to run on a web page?

If your interested in using the web-framework flask take a look at this , from this discussion on how to create a graph on-the-fly using matplotlib in flask.

I suggest storing the images somewhere like amazons s3 service.That way the images will be easy to access from session to session. So modifying the code from the thread link above...

return the objects as a png:

from matplotlib.pyplot import figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import StringIO

def create_a_graph()
    fig = figure(figsize=a_size_integer,a_size_integer))
    canvas = FigureCanvas(fig)
    canvas.print_png(png_output)

    return png_output

then upload them to amazons s3 site using boto and display them on what ever framework you choose.

A current project i'm working on does just this. Take a look at the files: graph.py, tools.py, views.py and base.html. Of course this requires understanding boto, flask and matplotlib but at least it gives you a starting place.

You should check out mod_wsgi and one of the many web-packages in existence from the python community or build your own.

But you also need to understand that tkinter is a gui-library it will not run from the web, which it sounds like is what you want. But instead you could build a webinterface for your code, which shouldn't be to hard if the presentation logic is well separated from the application-logic. That being said it's definitely not an easy task if you're new to programming in general or web-programming in particular.

Take a look at Flask as a lightweight framework for making a web application and then have a route somewhat like:

@app.route("/graph/<input>")
def drawGraph(input):
    # Graph drawing code goes here.
    image = # The image file as a string, as if you'd read it in from a file
            # object

    return Request(image, mimetype="image/png")

Then in the templates, you could use the /graph/ urls to fetch graphs for the appropriate input and render them in <img> tags.

The usual approach to problems like these is to separate the core of the program from the interface.

One approach is turning the program into a module which is then imported from both the GUI and web frameworks.

Another approach is to make the program into a daemon . Both the GUI and web interfaces then talk to it using some kind of messaging protocol. An example of this is eg ipython .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM