I am trying to run this simple python script through my terminal window:(it is the example on the bottlepy website)
from bottle import route, run, template
#route('/hello/:name')
def index(name='World'):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
In order to do so, I installed bottlepy from Terminal using
easy_install bottle
which instals it in /Library/Frameworks/Python.framework/Versions/4.2.30201/bin
When I try to run my python document in my terminal window:
cd ~/Dropbox/Work/MongoDB/
python hello.py
It returns:
ImportError: No module named bottle
I'm not sure what to do to put bottlepy in the correct emplacement so that it works. I'm sure it must be something silly but I can't think of what to do.
Thank you!!
xx
Take a look at the docs. You can download bottle.py directly to the folder with the rest of the progs, or install in the way shown.
In order to run the file mostly you need to create app first:
app = bottle.Bottle()
And later run it or serve by external web server like waitress
Related
I am trying to get the hang of Flask. I specifically want to learn using Flask with PyCharm.
I have set up the minimal example from the Flask website. When I run it from the terminal with flask run it works, which means I can open up the website.
When I run from PyCharm, it also seems to work, but I get an 404 error when I try to open up the site.
Am I missing some configuration in PyCharm?
Here's the app.py file:
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run(port=5000)
#app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
I believe in Pycharm everything runs in a virtual environment. That is why you cannot access your site. Try going to http://localhost:5000 while the app is running in Pycharm. If that doesn't work try it from the terminal. If it works in the terminal then come back to this answer and reply that it doesn't work.
I have come with python flask code which uses python libraries pywin32 and pYAD. I need to run these in production enviornment.
I tried to enable it over IIS through wfastcgi but its not working as expected.
Is there a way I can leverage the script to use in production set up, I have tried using Always Up converting script as a service but its not allowed in my organization similar with NSSM.
Is there any other way to explore this. Kindly help
Thanks
You can try waitress. You'll find the documentation Here.
Install Waitress
pip install waitress
setup.py file
from waitress import serve
import main
serve(main.app, host='0.0.0.0', port=8080) #'main.app' being your entry point
run setup.py and access your app through http://host_ip_address:8080
This is my test code running on Visual studio 2017, file name is test:
from flask import Flask, request #import main Flask class and request object
from test import app
app = Flask(__name__) #create the Flask app
#app.route('/query-example')
def query_example():
return 'Todo...'
#app.route('/form-example')
def formexample():
return 'Todo...'
#app.route('/json-example')
def jsonexample():
return 'Todo...'
if __name__ == '__main__':
app.run(debug=True, port=5000) #run app in debug mode on port 5000
But when I run it in Visual studio 2017 and I enter this route "http://127.0.0.1:5000/json-example" in Chrome Browser I always get this error message.
"404 Not Found"
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.
I am a learner, tried following this article:
https://scotch.io/bar-talk/processing-incoming-request-data-in-flask
Python: 3.6
Flask: 0.12.4
pip: 18.1
Earlier I used to get the return message with the same code but after updating the flask to 0.12.4, its not working, I think something has changed. I am not able to debug the exact issue.
--Update--
Ok, after updating the flask version to 1.0.2, but still not able to reach the webserver from the url: http://127.0.0.1:5000/json-example. Please check out the screenshot below.
I get 404 error not found:
Not Found
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.
The docs suggest that you should verify that .env & .flaskenv files are not interfering. (Likely they are not.)
shadowing
You really need to avoid shadowing one symbol with another.
Please don't name your module test, as python ships with a system library with that name. You might use test1 instead, to avoid the needless possibility of confusion.
Please rename your global variable to app_, since your module has an app.py file.
root URL
Please add a / slash route, even though it's not strictly necessary.
You will find that it aids debugging.
#app_.route('/')
def root():
return '<h1>top level</h1>'
version
Run a current version of flask, please.
It's much better to report problems with current rather than downrev code.
execution
Rather than having python directly call app_.run(...), please run flask instead. Use export or env, whichever you prefer:
$ export FLASK_APP=test1 FLASK_ENV=development
$ env FLASK_APP=test1 FLASK_ENV=development flask run --port=5000
This enables debug mode, which should help you get to the bottom of your routing issues.
Plus, reloading after edits is quite convenient.
The key is that rather than running python, you run flask which in turn runs python.
While I was troubleshooting, I noticed that the above same code works in raspberry Pi zero module with same python and flask version. But it is still not working in visual studio on windows 7. Probably there might be some issue with my visual studio. I will uninstall it reinstall it again. But yes my issue is solved for now. Thanks everyone for answering.
I had the same issue running VS Code on Ubuntu 19.10. The problem was returned string from route view function.
#app.route('/')
#app.route('/index')
def index():
return 'Hello World!'
gives error 404. But when I changed to :
#app.route('/')
#app.route('/index')
def index():
return '<h1>Hello World!</h1>'
everything was just fine.
I am using waitress to serve the web application content like.
waitress-serve --port=8000 myapp:application
While developing, as I change code, I continuously had to restart the waitress-serve to see my changes. Is there a standard way I can automate this?
I know this is an old question but I had a similar issue trying to enable hot reload functionality for my REST API using Falcon framework.
Waitress does not monitor file changes but you could use hupper on top of it. Pretty simple:
$ pip install hupper
$ hupper -m waitress --port=8000 myapp:application
It works on Windows too.
Based on the comment by #Dirk, I found the archive.org link to the snippet mentioned. I was able to get Waitress reloading by using Werkseug directly. Using Werkzeug's decorator run_with_reloader causes the app to restart whenever a Python file changes. (Werkzeug is used within Flask so it should be available).
Additionally, the app.debug = True line causes Flask to reload template files when they change. So you may want both considering your particular situation.
import werkzeug.serving
#werkzeug.serving.run_with_reloader
def run_server():
app.debug = True
waitress.serve(app, listen='127.0.0.1:4432')
if __name__ == '__main__':
run_server()
Once I had my server set up this way, it auto-reloaded the server whenever any file changed.
I am trying to have someone without Python installed use a simple program.
Have compiled it using py2exe, it worked, but it requires too much additional files. Have tried PyInstaller, but getting some errors when generating the executable file.
Anyway, I was thinking that it would be better upload that program to somewhere in the web, so, anyone with the link could use it in a much easier and practical way.
Does anyone know how I could accomplish that ?
If you want someone to download your file, use command:
python -m SimpleHTTPServer
on your file dir. And the someone use url {YourIp}:8000 with browser to access download.
If you want to execute something in python script, create a small web service use flask:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
# Do your python script here
return 'Hello World!'
if __name__ == '__main__':
app.run()
And also, the someone can execute your script use {YourIp}:8000 with browser.
Except for the code, all you need is a server and a browser to access.
Depending on how simple it is you could send them the file and run using an online interpreter such as http://repl.it/languages/Python3