Flask SQLAlchemy created an instance folder?
Question:
Flask app.
I have a database using sqlite called market.db
. I have configured it in my app.py
like this: app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
Then when I run flask shell
, it created a folder instance/
. Then I ran db.create_all()
, and that created a database inside of that instance/
folder. I can add items to the database no problems while in the same shell, but I am worried about this being production worthy.
File Hierarchy:
I want to deploy this eventually to a cloud server, and I want people to be able to login and register. What I am worried about is if when a user registers, it will save those changes throughout the entire website, or just on that person’s local machine. I want a way for when a user registers, to allow other people on the website to access those other users, because I will have a table of all of the users. Thank you.
app.py:
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
db = SQLAlchemy(app)
class Item(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(length=30), nullable=False, unique=True)
price = db.Column(db.Integer(), nullable=False)
barcode = db.Column(db.String(length=12), nullable=False, unique=True)
description = db.Column(db.String(length=1024), nullable=False, unique=True)
@app.route("/")
@app.route('/home')
def home_page():
return render_template('home.html')
@app.route('/market')
def market_page():
items = [
{'id': 1, 'name': 'Phone', 'barcode': '893212299897', 'price': 500},
{'id': 2, 'name': 'Laptop', 'barcode': '123985473165', 'price': 900},
{'id': 3, 'name': 'Keyboard', 'barcode': '231985128446', 'price': 150}
]
return render_template('market.html', items=items)
Answers:
What I am worried about is if when a user registers, it will save those changes throughout the entire website, or just on that person’s local machine.
No, it won’t save on the user’s local machine, remember you will be hosting your flask app on the cloud. the whole flask app along with the instance folder will be on the cloud, so it will save to the instance of the DB file on the cloud. The purpose of the instance folder is to hold sensitive information that is specific to that instance of your application which normally you don’t want to commit to a remote repository. This too does not happen automatically, it only happens if you add the instance folder to the .gitignore
file.
My problem is same. And this is a tutorial from Jim.
In the tutorial Jim’s DB is created in the working folder. But I have to create app_context and then only I create DataBase which only created in the instance folder.
please help. Thanks.
Flask app.
I have a database using sqlite called market.db
. I have configured it in my app.py
like this: app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
Then when I run flask shell
, it created a folder instance/
. Then I ran db.create_all()
, and that created a database inside of that instance/
folder. I can add items to the database no problems while in the same shell, but I am worried about this being production worthy.
File Hierarchy:
I want to deploy this eventually to a cloud server, and I want people to be able to login and register. What I am worried about is if when a user registers, it will save those changes throughout the entire website, or just on that person’s local machine. I want a way for when a user registers, to allow other people on the website to access those other users, because I will have a table of all of the users. Thank you.
app.py:
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
db = SQLAlchemy(app)
class Item(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(length=30), nullable=False, unique=True)
price = db.Column(db.Integer(), nullable=False)
barcode = db.Column(db.String(length=12), nullable=False, unique=True)
description = db.Column(db.String(length=1024), nullable=False, unique=True)
@app.route("/")
@app.route('/home')
def home_page():
return render_template('home.html')
@app.route('/market')
def market_page():
items = [
{'id': 1, 'name': 'Phone', 'barcode': '893212299897', 'price': 500},
{'id': 2, 'name': 'Laptop', 'barcode': '123985473165', 'price': 900},
{'id': 3, 'name': 'Keyboard', 'barcode': '231985128446', 'price': 150}
]
return render_template('market.html', items=items)
What I am worried about is if when a user registers, it will save those changes throughout the entire website, or just on that person’s local machine.
No, it won’t save on the user’s local machine, remember you will be hosting your flask app on the cloud. the whole flask app along with the instance folder will be on the cloud, so it will save to the instance of the DB file on the cloud. The purpose of the instance folder is to hold sensitive information that is specific to that instance of your application which normally you don’t want to commit to a remote repository. This too does not happen automatically, it only happens if you add the instance folder to the .gitignore
file.
My problem is same. And this is a tutorial from Jim.
In the tutorial Jim’s DB is created in the working folder. But I have to create app_context and then only I create DataBase which only created in the instance folder.
please help. Thanks.