栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Experimenting in Interactive Mode

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Experimenting in Interactive Mode

You can get to by simply starting your terminal and entering python3 to run the code.
Steps:

  1. Rename your app from flask-example.py to flask_example.py (replace the dashes with underscores, so that we can import it with Python)
  2. Go to your terminal and enter Python interactive mode by entering python3
  3. import the app by running import flask_example (notice that there is no .py at the end when we are imprting!)
  4. If you get the deprecating warning, modify the app to set app.config[‘SQLALCHEMY_TRACH_MODIFICATIONS’] = False
  5. Try importing again - you should now get a successful import with no errors or warnings.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATAbase_URI'] = 'postgresql://username@localhost:5432/example'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

# The Person class inherit from db.Model, we wound up linking and connecting to SQLAlchemy's mappings between classes and tables.
class Person(db.Model):
    # By defult, SQLAlchemy will pick the name of the table for you and set it equal to the lowercase version of your class.
    # But if you want to control the name of the table, you can do this way
    __tablename__ = 'persons'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)

db.create_all()

@app.route('/')
def index():
    # Get the first record of the Persons
    person = Person.query.first()
    return 'Hello ' + person.name

if __name__ == '__main__':
    app.run()


How to debug?
使用这个函数:

# Ability to customozie a principle string (useful for debugging)
def __repr__(self):
	return f'<{self.id}, {self.name}>'

这个代码:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATAbase_URI'] = 'postgresql://username@localhost:5432/example'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

# The Person class inherit from db.Model, we wound up linking and connecting to SQLAlchemy's mappings between classes and tables.
class Person(db.Model):
    # By defult, SQLAlchemy will pick the name of the table for you and set it equal to the lowercase version of your class.
    # But if you want to control the name of the table, you can do this way
    __tablename__ = 'persons'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)

    def __repr__(self):
        return f''

db.create_all()

@app.route('/')
def index():
    # Get the first record of the Persons
    person = Person.query.first()
    return 'Hello ' + person.name

if __name__ == '__main__':
    app.run()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/499237.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号