I'm trying to follow the instructions given here: Adding a favicon to a Flask server without HTML on how to add a favicon to a flask app, however its not working for me. Here is my application file:
from flask import Flask,send_from_directory
application=Flask(__name__)
#application.route('/')
def main():
return '<html><p>hello world</p></html>'
#application.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(application.root_path, 'static'),
'favicon.ico',mimetype='image/vnd.microsoft.icon')
if __name__=='__main__': application.run(debug = True)
and here is my directory structure:
➜ demo ls -R
application.py static
./static:
favicon.ico
When I run the application in Firefox, no favicon is shown, and when I run it in Chrome, the default favicon is shown. I used this website to convert a png into an ico file:
https://www.freeconvert.com/png-to-ico
Please let me know where I'm going wrong.
When I run the app in chrome, I get this error in the console:
GET http://localhost:5000/favicon.ico 500 (INTERNAL SERVER ERROR)
I needed to import os, here is the working application:
from flask import Flask,send_from_directory
import os
application=Flask(__name__)
#application.route('/')
def main():
return '<html><p>hello world</p></html>'
#application.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(application.root_path, 'static'),
'favicon.ico',mimetype='image/vnd.microsoft.icon')
if __name__=='__main__': application.run(debug = True)
Related
I am developing an app and the development setup was really easy.
I run it locally with:
$ . .venv/bin/activate
(.venv) $
(.venv) $ python -m flask run
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:8080
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: -###-###
and I have configured apache2 on my (ubuntu) laptop with:
ProxyPass / http://127.0.0.1:8080
My code is structured like:
app.py
pages/scc_main/scc.html
...
The code has this:
import re
import jinja2
from flask import Flask
from flask import request
import data
app = Flask(__name__)
env = jinja2.Environment(loader=jinja2.FileSystemLoader("pages"))
#app.route('/')
def hello_world():
return '<h2>Hello, World!</h2>'
#app.route('/contracts/scc')
#app.route('/contracts/scc/')
def contracts_main():
main = env.get_template('scc_main/scc.html')
context = data.build('scc_main')
return main.render(**context)
And everything works great. As in:
$ curl 'http://localhost/'
<h2>Hello, World!</h2>$
But when I deploy. Wow. I set my site's root to point to the app. That is actually working. I can hit https://opencalaccess.org/ and it gets my static content.
I have:
import sys
import logging
logging.basicConfig(
level=logging.DEBUG,
filename='/var/www/<full-path>/logs/contracts_scc.log',
format='^(asctime)s %(message)s')
sys.path.insert(0, '/var/www/<full-path>')
sys.path.insert(0, '/var/www/<full-path>/.venv/lib/python3.8/site-packages')
And https://opencalaccess.org/contracts/scc works. But only after I change the Environment call above to:
env = jinja2.Environment(loader=jinja2.FileSystemLoader("/var/www/full-path>/pages"))
Now, any link which is just a link is fine. But anything that looks at the flask.request.path gives me:
The browser (or proxy) sent a request that this server could not understand.
What the heck? Setting up the dev environment was so easy. What do you have to do to get this working in deployment? Any suggestions?
ADDED:
Well, it seems clear that it is the WSGI part that is having the problem. My script is not receiving the request structure and so it cannot read any parameters. I have all my parameters on the URL, so my data building method reads the request.path to see what to do.
So, where to go from here. We will see.
I am no longer able to reproduce this.
I am building a flask application and the main file of this application is named app.py
which is like this:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import Development
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(Development)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
##app.route('/')
#def index():
# return "Blog Home"
from mod_admin import admin
from mod_users import users
app.register_blueprint(admin)
app.register_blueprint(users)
but i am trying to put all views in a file which is named views.py and looks like this:
from app import app
#app.route('/')
def index():
return "Blog Home"
both app.py and views.py are in the same directory. when I run :
flask run
I will get the url and when I put it in the browser (tried a couple of them including firefox and Chrom), I will get 404 error. however if I remove views.py and uncomment the view in the app.py it works perfectly and I can see the message on the browser.
do you know what could be the issue when I put the view in views.py and remove that from app.py?
Remove the app import from views and you need to use a view blueprint instead.
Views.py should look something like this:
bp = Blueprint('views', __name__, url_prefix='/', template_folder='./templates')
#bp.route('/')
def homepage():
return "blog home"
and app.py should have the lines:
from views import bp
app.register_blueprint(bp)
I am trying to setup simple flask application to perceive app_context() feature.
This is my code:
# config.py
UPDATE_PERIOD = 100.0
# init.py
from flask import Flask
import config
app = Flask(__name__)
app.config.from_object(config)
app.config['DEBUG'] = True
#app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == "__main__":
app.run()
# ex.py
from flask import current_app
from flask import Flask
app = Flask(__name__)
with app.app_context() as app:
print(current_app.config['DEBUG'])
print(current_app.config['UPDATE_PERIOD'])
Now, I am running my init.py:
python3 init.py
* Serving Flask app "init" (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: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 666-659-182
which is okay, how it should be.
Here I expect that app.config['DEBUG'] = True and app.config['UPDATE_PERIOD'] = 100.0
And I am trying to extract this vars in ex.py.
The problem is: when I execute: python3 ex.py I expect this:
True
100.0
but eventually I am getting this:
False
Traceback (most recent call last):
File "ex.py", line 10, in <module>
print(current_app.config['UPDATE_PERIOD'])
KeyError: 'UPDATE_PERIOD'
so I cannot get how app_context() feature is working whereas I think I am doing the same things as written in docs.
There is an option to import in ex.py:
from init import app
but I want to get working instance without complex import since my application is quite big.
Can anyone please help me with this..?
Thank you in advance!
I am writing a flask app which follows the following folder structure.
backend
server.py
__init__.py
test
__init__.py
servertest.py
"""Server ping test for flask"""
import flask
import pytest
from server import app
#pytest.fixture()
def client():
yield testing.TestClient(app)
def test_ping_resource(client):
doc = "Flask GET service is working"
result = client.simulate_get("/api/ping")
assert result.status_code == 200
assert result.json == doc
And this is my test file. When I am running the file. it gives
from server import app
E ImportError: No module named 'server'
What am I doing wrong, which is making the server module invisible to test module?
When you run a python file, its parent folder is added to the python path, but the parent of its parent (here, your root folder) is not!
What you could do is:
running your tests from the root folder (preferred)
Using relative imports, here: from ..server import app
Modify your python path (not advised)
Python imports modules from a list of path:
import sys
sys.path
You have to provide Python a way to find server.py:
you can run your code from the backend directory, because the current path is automatically happened to sys.path (cd backend; pytest)
you can add the full path to backend to PYTHONPATH
At some point, you will need to read the doc about import.
Also, to use pytest naturally, your test files must start with test.
I have a basic flask-restful application with a structure that looks like this as recommended on the flask website.
/application
/application
/config.py
/__init__.py
/wsgi.ini
Slightly irrelevant, but config.py is generated by our CD server.
wsgi.ini looks likes this
[uwsgi]
module = application:app
master = true
processes =5
socket = /tmp/application.sock
chmod-socket = 660
vacuum = true
die-on-term = true
and __init__.py looks something like this
import config
from flask import Flask, request, g
from flask_restful import Resource, Api, abort
app = Flask(__name__)
api = Api(app)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Now when I try to startup the application using uwsgi --ini wsgi.ini I get this error:
File "./application/__init__.py", line 2, in <module>
import config
Originally __init__.py was called main.py and I executed that to debug, which is why the import is now wrong. I'm guess I will need to change the import to be from .config import *
My question is two fold:
Could I have avoided the import problem completely. ie is there a way in python3 to import sibling modules that will work for both approaches
is my wsgi.ini in the correct place, or should it be in the inner application directory?
Python 3.x dropped support for infra-package relative imports. You need to use an absolute import:
from application import config
or the new spelling of relative imports:
from . import config
If you need to also support legacy versions of Python you can enable this behavior with:
from __future__ import absolute_import