I have flask app that uses geodis which has dependency on redis that acts as cache for city mapped to latitude and longitude from geodis.
I have this code that needs to be run just once on deployment of the flask web app on heroku,
from geodis.provider.geonames import GeonamesImporter
import geodis
fileName = os.path.split(geodis.__file__)[0] + "/data/cities1000.json"
importer = GeonamesImporter(fileName, os.getenv("REDIS_HOST"), os.getenv("REDIS_PORT"), 0)
importer.runimport()
How can I have it setup to run once on deployment?
i think one way is to use application initialization function.
if __name__ == "__main__":
fileName = os.path.split(geodis.__file__)[0] + "/data/cities1000.json"
importer = GeonamesImporter(fileName,
os.getenv("REDIS_HOST"),
os.getenv("REDIS_PORT"), 0)
importer.runimport()
app.run(host='0.0.0.0', port=app.config['PORT'])
this would run it before creating the app.
Related
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'
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?
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)
I have whittled a much-larger Flask app down to the essentials in order to try to figure out why dateparser (0.7.6 and also 1.0.0) is not working in Flask (Flask 1.1.2, Werkzeug 1.0.1) under Python 3.8.5 in Windows 10.
run.py
from mytest import app as _application
def application(environ, start_response):
return _application(environ, start_response)
if __name__ == '__main__':
from werkzeug.serving import run_simple
run_simple('localhost', 5003, application, use_reloader=True, use_debugger=True, threaded=True, use_evalex=True)
mytest.py
import os, dateparser, time
from flask import Flask
app = Flask(__name__)
# os.environ['TZ'] = 'UTC'
#app.route('/')
def date_test():
parsed = dateparser.parse('01/13/2021')
return 'Date parse results are: {}'.format(parsed)
This app runs fine - going to http://127.0.0.1:5003 produces "Date parse results are: 2021-01-13 00:00:00"
However, if I uncomment line 5 in mytest.py (os.environ['TZ'] = 'UTC'), dateparser suddenly stops working and the URL returns "Date parse results are: None"
There are some ways I've found to mitigate it:
In run.py, move the from mytest import app as _application line into the application function
In mytest.py, move the os.environ['TZ']... line into the date_test function
but I don't really understand why the problem is happening in the first place, or why either of those two "solutions" actually solves the problem.
What am I missing?
I'm relatively new to python and am trying to build a flask server. What I would like to do is have a package called "endpoints" that has a list of modules where each module defines a subset of application routes. When I make a file called server.py with the following code this works like so
import os
from flask import Flask
app = Flask(__name__)
from endpoint import *
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)
Right now there's only one endpoint module called hello.py and it looks like this
from __main__ import app
# a simple page that says hello
# #app.route defines the url off of the BASE url e.g. www.appname.com/api +
# #app.route
# in dev this will be literally http://localhost:5000/hello
#app.route('/hello')
def hello():
return 'Hello, World!'
So... the above works when I run python server.py, the issue happens when I try to run the app using flask.
Instead of server.py it just calls __init__.py which looks like this
import os
from flask import Flask
# create and configure the app
# instance_relative_config states that the
# config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
from endpoint import *
When I run flask run in terminal I get ModuleNotFoundError: No module named 'endpoint'
but again if I change the code so it looks like the following below, then flask run works.
import os
from flask import Flask
# create and configure the app
# instance_relative_config states that the
# config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
# #app.route defines the url off of the BASE url e.g. www.appname.com/api +
# #app.route
# in dev this will be literally http://localhost:5000/hello
#app.route('/hello')
def hello():
return 'Hello, World!'
I'm pretty sure this is happening because I don't fully understand how imports work...
So, how do I set up __init__.py so that it imports all the modules from the "endpoint" package and works when I call flask run?
When you use a __init__.py file, which you should, Python treats the directory as a package.
This means, you have to import from the package, and not directly from the module.
Also, usually you do not put much or any code in a __init__.py file.
Your directory structure could look like this, where stack is the name of the package I use.
stack/
├── endpoint.py
├── __init__.py
└── main.py
You __init__.py file is empty.
main.py
import os
from flask import Flask
# create and configure the app
# instance_relative_config states that the
# config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
from stack.endpoint import *
endpoint
from stack.main import app
# a simple page that says hello
# #app.route defines the url off of the BASE url e.g. www.appname.com/api +
# #app.route
# in dev this will be literally http://localhost:5000/hello
#app.route('/hello')
def hello():
return 'Hello, World!'
You can then run your app...
export FLASK_APP=main.py
# followed by a...
flask run
This all said, when I create a new Flask app, I usually only use one file, this makes initial development easier, and only split into modules when the app really grows bigger.
Also, for separating views or let's call it sub packages, Flask offers so called Blue prints. This is nothing you have to worry about right now, but comes especially handy when trying to split the app into sub applications.
I am trying to set up a Flask web app using Elastic Beanstalk on AWS. I have followed the tutorial here and that works fine. I am now looking to expand the Flask webapp, and this works fine, until I import scipy.spatial as spatial, when this is part of my import statements, running eb open just times out. I receive
>>>> HTTP ERROR 504
running the webapp locally works absolutely fine even with the scipy import, it is only when I try and deploy to beanstalk that it doesn't want to work. Below is my code
import os
import requests
from bs4 import BeautifulSoup
import pandas as pd
import scipy.spatial as spatial ##### Removing this and everything works!
from flask import Flask
from flask_cors import CORS
from flask_restful import Resource, Api
from flask_jsonpify import jsonify
# print a nice greeting.
def say_hello(username = "World"):
df = pd.DataFrame({"a":[1,2,3]})
return '<p>Hello %s!</p>\n' % username
# some bits of text for the page.
header_text = '''
<html>\n<head> <title>EB Flask Test</title> </head>\n<body>'''
instructions = '''
<p><em>Hint</em>: This is a RESTful web service! Append a username
to the URL (for example: <code>/Thelonious</code>) to say hello to
someone specific.</p>\n'''
home_link = '<p>Back</p>\n'
footer_text = '</body>\n</html>'
# EB looks for an 'application' callable by default.
application = Flask(__name__)
# add a rule for the index page.
application.add_url_rule('/', 'index', (lambda: header_text +
say_hello() + instructions + footer_text))
# add a rule when the page is accessed with a name appended to the site
# URL.
application.add_url_rule('/<username>', 'hello', (lambda username:
header_text + say_hello(username) + home_link + footer_text))
# run the app.
if __name__ == "__main__":
# Setting debug to True enables debug output. This line should be
# removed before deploying a production app.
application.debug = True
application.run()
I have tried increasing the command timeout for the environment from 600 to 900, although the timeout error occurs well before 600 seconds has elapsed.
Right, I am not sure why this is the case but I updated the version of scipy in my requirements.txt and the app is working as expected!
Originally I had
scipy==1.4.1
Now I have
scipy==1.2.3
I have no idea why this has fixed the deployment issue, especially given 1.4.1 works perfectly locally. If anyone has an idea, or if this a bug I should be reporting it would be good to know!
In my main.py have the below code:
app.config.from_object('config.DevelopmentConfig')
In another module I used import main and then used main.app.config['KEY'] to get a parameter, but Python interpreter says that it couldn't load the module in main.py because of the import part. How can I access config parameters in another module in Flask?
Your structure is not really clear but by what I can get, import your configuration object and just pass it to app.config.from_object():
from flask import Flask
from <path_to_config_module>.config import DevelopmentConfig
app = Flask('Project')
app.config.from_object(DevelopmentConfig)
if __name__ == "__main__":
application.run(host="0.0.0.0")
if your your config module is in the same directory where your application module is, you can just use :
from .config import DevelopmentConfig
The solution was to put app initialization in another file (e.g: myapp_init_file.py) in the root:
from flask import Flask
app = Flask(__name__)
# Change this on production environment to: config.ProductionConfig
app.config.from_object('config.DevelopmentConfig')
Now to access config parameters I just need to import this module in different files:
from myapp_init_file import app
Now I have access to my config parameters as below:
app.config['url']
The problem was that I had an import loop an could not run my python app. With this solution everything works like a charm. ;-)