0

I have written my 1st Python API project with Flask and now am trying to deploy it to Netlify.

Searched online and found I need to use Flask-Frozen to generate a static website. Not sure I'm doing it correctly, because my project is a API project not a website project, so may be I should not use Flask-Frozen(FF)?

But if I could still use FF to generate static website for my API project, here is my project:

--

  • app.py
  • mazesolver
    • mazeapi.py

Here is the app.py

from flask_frozen import Freezer
from mazesolver import mazeapi

# Call the application factory function to construct a Flask application
# instance using the development configuration
# app = mazeapi()

# Create an instance of Freezer for generating the static files from
# the Flask application routes ('/', '/breakfast', etc.)
freezer = Freezer(mazeapi)


if __name__ == '__mazeapi__':
    # Run the development server that generates the static files
    # using Frozen-Flask
    freezer.run(debug=True)

mazeapi.py

import io
from mazesolver.solver import MazeSolver
from markupsafe import escape
from flask import Flask, flash, request, redirect, send_file
from werkzeug.utils import secure_filename
    
ALLOWED_EXTENSIONS = { 'png', 'jpg', 'jpeg' }

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024

@app.route('/maze/<mazename>')
def maze(mazename):
    return 'maze 4 %s' % escape(mazename)

Whenever I run: python app.py

I got this error:

Traceback (most recent call last):
  File "app.py", line 10, in <module>
    freezer = Freezer(mazeapi)
  File "/home/winstonfan/anaconda3/envs/maze/lib/python3.7/site-packages/flask_frozen/__init__.py", line 98, in __init__
    self.init_app(app)
  File "/home/winstonfan/anaconda3/envs/maze/lib/python3.7/site-packages/flask_frozen/__init__.py", line 108, in init_app
    self.url_for_logger = UrlForLogger(app)
  File "/home/winstonfan/anaconda3/envs/maze/lib/python3.7/site-packages/flask_frozen/__init__.py", line 588, in __init__
    self.app.url_default_functions.setdefault(None, []).insert(0, logger)
AttributeError: module 'mazesolver.mazeapi' has no attribute 'url_default_functions'
Franva
  • 6,565
  • 23
  • 79
  • 144

2 Answers2

0

I had the same issue and added

url_default_functions={}

to the app, right before

if __name__ == "__main__":

    app.run()

I guess you could put it right after

app = Flask(__name__)

and the error went way... but there are many more :) It seems that frozen-flask isn't working with the lastest flask, but I couldn't get any other versions working with it.

Did you manage to get it working?

sur.la.route
  • 440
  • 3
  • 11
0

You have a simple namespace bug.

In the code you've posted, you are freezing your module mazeapi, not the actual Flask application, mazeapi.app.

Try changing your Freezer call in line 10 of app.py to:

freezer = Freezer(mazeapi.app)

foundling
  • 1,695
  • 1
  • 16
  • 22