dimanche 31 octobre 2021

Matplotlib causing stuck web server

I have created a web server application on Replit using flask and it needs to use matplotlib library. The problem is that it gets stuck every time the server starts. This seems to be solved by removing matplotlib from the code.

This is what I get on the console before it freezes:

Matplotlib created a temporary config/cache directory at /tmp/matplotlib-jvbhc0qv because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.

Python 3.8.2 (default, Feb 26 2020, 02:56:10)
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://172.18.0.191:8080/ (Press CTRL+C to quit)

enter image description here

As you can see, it shows a screen when it's supposed to show the matplotlib output, but I don't need to show anything on the editor screen. I have also tried to change the backend of matplotlib but it doesn't work either.

This is the function that uses the library, it's only creating an image to send it to the client, it's not showing any graph on screen:

@app.route('/function', methods=['POST'])
def Function():
    data = request.data.decode()
    doc = nlp(data)
    
    fig = plt.figure(figsize=(8, 8))
    wc = WordCloud(background_color="white", 
               #max_words=350,
               font_path='GELION-BOLD.TTF',
               width=1000, 
               height=600, 
               ).generate(doc.text)
    plt.imshow(wc)
    plt.axis("off")
    buf = io.BytesIO()
    fig.savefig(buf, format='png')
    buf.seek(0)
    string = base64.b64encode(buf.read())
    
    uri = 'data:image/png;base64,' + urllib.parse.quote(string)
    html = '<img src = "%s"/>' % uri
    return html

The only way I managed to "solve" this is by creating a new Repl and transferring all the code, but this solution only works one time before it gets stuck again. Any solution would be very helpful.




Aucun commentaire:

Enregistrer un commentaire