I am new to web dev (Flask) and I have seen different developers defining the app variable differently.
Some use
app = Flask(__name__)
While others use
app = create_app()
What is the difference between the 2?
Related
I've been doing backend programming for years but I've now decided to finally tackle frontend. I've watched two tutorials on using AWS amplify and one thing I don't understand is that I can only get my app to run on my localhost when I type
flask run
or
python flask_app.py
I have my code published on Github and AWS amplify says that it successfully reads it. However I don't see why the app would deploy to my webpage when I never actually run the flask app. I'm assuming that AWS amplify puts the code from github onto a Linux machine but don't I have to SSH into that machine and write: python flask_app.py?
I'm not receiving any error messages but my webpage is still blank and I'm guessing that that is because I see no reason why any of the files would be executed if they are just sitting there on some computer out there. Also, should I be concerned about this? In both of the tutorials I have watched there is an additional check mark which says 'verified' but not in my case.
Here is my code:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
#app.route('/home')
def home():
return "Help me be swank"
#app.route('/about')
def about():
return "I'm swank"
app.run(debug=1)
I'm wondering if I could use multiple backend frameworks, using only 1 database in a VM instance in production without any problems. For example, I wanna keep Django Admin and its authentication rest API services, while using FastAPI for other things, possibly even ExpressJs in the future.
I want to achieve this because different frameworks have different strengths and available third-party libraries in their ecosystems.
I know NGINX as a reverse proxy and a load balancer will control the traffic load and directions to different endpoints. Still, would there be any conflicts in the database or anything I should be watching out for?
Thank you!
You can create a dedicated path and mount it on app init. It works with flask, so I guess django should work as well
Here's an example taken from the docs
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from flask import Flask, escape, request
flask_app = Flask(__name__)
#flask_app.route("/")
def flask_main():
name = request.args.get("name", "World")
return f"Hello, {escape(name)} from Flask!"
app = FastAPI()
#app.get("/v2")
def read_main():
return {"message": "Hello World"}
app.mount("/v1", WSGIMiddleware(flask_app))
See the documentation at https://fastapi.tiangolo.com/advanced/wsgi/
I am trying to create a simple Fibonacci web service with
* Django and
* Flask
I am using djangorestframework and flask_restful
I am doing this to make some comparison.
with Flask I am able to specify
the number of process or
to use threading etc. with Api in flask_restful
from flask import Flask, request, Response, jsonify
from flask_restful import abort, Resource, Api, reqparse
api = Api(app)
api.add_resource(Test, '/test/')
app.run(port=1234, host='0.0.0.0', threaded=True) #processes=50)
My question, how to specify the same in django rest framework?
From the docs I am not able to find a straight forward answer, yet..
That's because it's two different matters.
Django only provides a development server which obviously is for development and single thread/process.
If you want to tune the number of process, you'll need to run the Django project through gunicorn / uwsgi / mod_wsgi / ..., each of those lets you tune the process / threads.
Run the Django project through uwsgi or mod_wsgi.
I'm playing with combining a couple flask apis that I have into one application that can be a little easier to deploy and set up for devs instead of needing three separate applications running. Currently each api resides on a separate port. I'm trying to use the DispatcherMiddleware to run all three applications but so far it seems like you can only use prefixes like
from frontend import app as frontend
from TestApi import app as test
from DevApi import app as dev
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
app = DispatcherMiddleware(frontend, {
'/test': test,
'/dev': dev
})
run_simple('localhost', 4000, app, use_reloader=True)
now all my services run on 4000 but what id like to have is something like this
from frontend import app as frontend
from TestApi import app as test
from DevApi import app as dev
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
app = DispatcherMiddleware(frontend, {
':5000': test,
':6000': dev
})
#frontend runs on 4000, test runs on 5000, dev runs on 6000
run_simple('localhost', 4000, app, use_reloader=True)
Am I just asking for something that makes no sense or is there a way to accomplish using this or another setup.
I've come to the conclusion that this is a stupid idea that is very fragile and requires a lot of unnecessary complexity. I've instead just opted to write a bash script that starts all the apps as separate wsgi instances on their own port.
The Flask documentation on application dispatching contains no hint on port based dispatching. As you mentioned you can simply start separate wsgi instances, as far as I know this is the only possible way using Werkzeug.
Reference: http://flask.pocoo.org/docs/latest/patterns/appdispatch/
I'm working on a Django 1.2 app and I'm a kind of beginner with the framework. I want to split my tests in several files for the app https://github.com/vkhemlan/BolsaTrabajo/tree/master/bolsa_trabajo, how I can do that? What configurations do I have to do?
Here's a really good guide on testing django apps:
A Guide to Testing in Django
And the example app on github splits the tests for forms, views and models into separate files, so it is probably a good example for you.
Note how each test module gets imported in __init__.py:
from polls.tests.forms import *
from polls.tests.models import *
from polls.tests.views import *