I have successfully created my virtual environment and installed flask but everytime I run the program I get "Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." when I run it on REPL it works.
from flask import Flask, render_template
app = Flask(__name__)
app.route('/')
def index():
return '<p>Hello Flask</p>'
if __name__ == '__main__':
app.run(debug=True)
* Detected change in 'c:\\Users\\mavhu\\Desktop\\Flask_Blogging_App\\main.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 398-868-074
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [03/Dec/2020 16:59:06] "?[33mGET / HTTP/1.1?[0m" 404 -
127.0.0.1 - - [03/Dec/2020 16:59:11] "?[33mGET / HTTP/1.1?[0m" 404 -
CODE SCREENSHOT
Check the following steps:
Did you use this command before running the Flask app : set FLASK_APP=main.py
If you are running on Windows, disable any Firewall or AntiVirus solution. Sometimes, they block all such connections on localhost.
Also try using the URL http://localhost:5000
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.
This seems like a problem a lot of folks have.
I completely changed my index.html page. Then saved changes, stopped and re-ran server.py which has the following code:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def home():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)
I have saved changes, stopped and re-run flask from terminal using >> set FLASK_APP=server.py >> flask run.
I have also tried clearing browser cache and retried on multiple browsers. I have tried shift+reload
Previously it used to work on completely closing Pycharm and restarting. However even that isn't working anymore.
Apart from displaying text that no longer exists in index.html, the Debugger Console is also showing the following error:
127.0.0.1 - - [22/Jul/2021 17:29:58] "GET /favicon.ico HTTP/1.1" 404 -
My pointer to the file location is correct:
But just to make sure I copy pasted favicon.ico in every damn folder in the project.
I have tried "save all" on my project several times. Stopped and started debugger several times. Any other solution I left out?
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 have an Node.js app with a Python script that uses Selenium webdriver.
When I ran it locally everything is working as expected. When deployed to Heroku the web browser fail to open from app.
Here is my code:
browser = webdriver.Firefox(executable_path='C:\\path\\to drivers\\geckodriver.exe')
browser.minimize_window()
browser.maximize_window()
browser.get('http://www.google.com')
The browser console error:
HTTP500: SERVER ERROR - The server encountered an unexpected condition that prevented it from fulfilling the request.
Update:
Based on suggestion from Chris I updated the code to:
browser = webdriver.Chrome()
and added google-chrome buildpack.
But still can't open browser when running web version.
Heroku log mentions PATH. But drivers are in PATH. everything works locally.
Obviously, I am missing something.
2019-08-07T11:27:54.690913+00:00 app[web.1]: { Error: selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Any guidance is greatly appreciated.
browser = webdriver.Firefox(executable_path='C:\\path\\to drivers\\geckodriver.exe')
You definitely won't have a geckodriver.exe at that location on Heroku. Dynos don't run Windows, and Heroku doesn't provide graphical browsers by default.
You'll have to add an appropriate buildpack, e.g.
The Heroku-maintained Google Chrome buildpack or
This Firefox buildpack from the community
and modify your code so you aren't hard-coding a path to a specific executable. If the executable is on your PATH you should just be able to do something like
driver = webdriver.Chrome() # or
driver = webdriver.Firefox()
I'm unable to connect to CherryPy server running inside a docker container from my system when i use cherrypy.tree.mount but when I do cherrypy.quickstart() I can connect to the server. A curl request to localhost:8080 with cherrypy.tree.mount gives a curl: (56) Recv failure: Connection reset by peer error.
App file which works
import cherrypy
class HelloWorld(object):
#cherrypy.expose
def index(self):
return "Hello World!"
cherrypy.quickstart(HelloWorld(), '/', {'global': {'server.socket_host':'0.0.0.0','server.socket_port': 8080}})
App file which fails
import cherrypy
class HelloWorld(object):
#cherrypy.expose
def index(self):
return "Hello World!"
cherrypy.tree.mount(HelloWorld(), '/', {'global':{'server.socket_host':'0.0.0.0','server.socket_port': 8080}})
cherrypy.engine.start()
cherrypy.engine.block()
Dockerfile
FROM python:3.6
RUN mkdir -p /opt/server
WORKDIR /opt/server
ADD . /opt/server
VOLUME /opt/server
RUN pip install cherrypy
EXPOSE 8080
CMD python app.py
I have to use cherrypy.tree.mount because I have to run multiple applications on the same server.
I was giving the configuration wrong. The right way to set global configs is:
cherrypy.config.update({'server.socket_host':'0.0.0.0','server.socket_port': 8080})
once that was set it is working fine. configs on cherrypy.tree.mount is for each application.