I am refreshing my browser...after running but,
changes are not affected in browser
from flask import Flask, send_file, render_template, send_file
app = Flask(__name__)
#app.route('/')
def hello_world():
return send_file('templates/create_conf_view.html')
if __name__ == '__main__':
app.run(debug=True, use_reloader=True, host='0.0.0.0', port=1672,threaded=True)
What i am doing wrong ?
According to your code you need to have a directory structure as follows:
app
|_
run.py
templates/
|_create_conf_view.html
According to docs you should not pass templates when rendering a template.
Change your original file to and see if it works:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def hello_world():
return render_template('create_conf_view.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=1672, thread=True)
Also be sure to access your application at specified port!
Related
I am using flask with blueprints each blueprint is single subdomain. What I am trying to do is to redirect from one blueprint route to another subdomain.
Simplified file structure:
|-about_domain
| |-__init__.py
|-store_domain
|-__init__.py
wsgi.py
wsgi.py:
from flask import Flask, redirect, url_for
from store_basicwear import store
from about_basicwear import about
app = Flask(__name__, subdomain_matching=True, static_folder='static')
app.register_blueprint(store)
app.register_blueprint(about)
#app.route("/")
def default():
return redirect(url_for('store.index'))
if __name__ == '__main__':
app.run()
/store_domain/__init__.py:
from flask import Blueprint, render_template
store = Blueprint('store', __name__, subdomain='store')
#store.route('/about')
def about():
return # redirect to about.index
/about_domain/__init__.py:
from flask import Blueprint, render_template
about = Blueprint('about', __name__, subdomain='about')
#about.route('/')
def index():
return render_template('main.html')
How I want it to work:
store.domain/about ---(redirect)---> about.domain/
I want to avoid circular import as well as hardcoded routes. Is it possible, maybe by current_app?
How do I run flask app directly to 0.0.0.0/input page instead of "/"?
Is this possible?
Otherwise, what is best way to exclude "/" from showing?
#app.route("/")
def home():
return 200
if __name__ == '__main__':
app.run(host='0.0.0.0/input', port=int(os.getenv("FLASK_PORT", "8100")), debug=True)
Redirect your root url:
from flask import Flask, redirect, url_for
app = Flask(__name__)
#app.route("/")
def home():
return redirect(url_for("hello"))
#app.route("/hello")
def hello():
return "<p>Hello</p>"
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
I have been following the methods provided in this post:
https://stackoverflow.com/questions/12167729/flask-subdomains-with-blueprint-getting-404
Here is my relevant code:
__init__.py
from flask import Flask, render_template
app = Flask(__name__, static_url_path="", static_folder="static"
import myApp.views
views.py
from flask import Flask, render_template, request, Blueprint
from myApp import app
app.config['SERVER_NAME'] = 'myapp.com'
app.url_map.default_subdomain = "www"
#app.route("/")
def index():
return "This is index page."
test = Blueprint('test', 'test', subdomain='test')
#test.route("/")
def testindex():
return "This is test index page."
app.register_blueprint(test)
if __name__ == "__main__":
app.run()
Using this method I am able to get subdomains 'test' and 'www' working, but now I am unable to access my website via IP address.
Any guidance into the right direction would be appreciated!
I have the following structure in my project
\ myapp
\ app
__init__.py
views.py
run.py
And the following code:
run.py
from app import create_app
if __name__ == '__main__':
app = create_app()
app.run(debug=True, host='0.0.0.0', port=5001)
views.py
#app.route("/")
def index():
return "Hello World!"
_init_.py
from flask import Flask
def create_app():
app = Flask(__name__)
from app import views
return app
I'm trying to use the factory design pattern to create my app objects with different config files each time, and with a subdomain dispatcher be able to create and route different objects depending on the subdomain on the user request.
I'm following the Flask documentation where they talk about, all of this:
Application Context
Applitation Factories
Application with Blueprints
Application Dispatching
But I couldn't make it work, it seems that with my actual project structure there are no way to pass throw the app object to my views.py and it throw and NameError
NameError: name 'app' is not defined
After do what Miguel suggest (use the Blueprint) everything works, that's the final code, working:
_init.py_
...
def create_app(cfg=None):
app = Flask(__name__)
from api.views import api
app.register_blueprint(api)
return app
views.py
from flask import current_app, Blueprint, jsonify
api = Blueprint('api', __name__)
#api.route("/")
def index():
# We can use "current_app" to have access to our "app" object
return "Hello World!"
from flask import Flask, render_template
app = Flask(__name__, static_url_path='')
#app.route('/')
def index():
return render_template('index.html')
#app.route('/page/<path:page>')
def article(page):
return render_template('page.html')
if __name__ == "__main__":
app.run()
Work just fine. But if i change the second route to #app.route('/<path:page>'), then any access to URL like /path/to/page yields 404.
Why doesn't #app.route('/<path:page>') work?
Related questions, that don't answer the question however:
Flask: Handle catch all url different if path is directory or file
Custom routing in Flask app
Capture arbitrary path in Flask route
static_url_path conflicts with routing. Flask thinks that path after / is reserved for static files, therefore path converter didn't work. See: URL routing conflicts for static files in Flask dev server
works flawless for me:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/page/<path:page>')
def article(page):
return render_template('page.html')
if __name__ == "__main__":
app.debug = True
app.run()
I can access: http://localhost/ -> index and http://localhost/page/<any index/path e.g.: 1> -> article