利用jupyter的cell是可以运行python文件的:

运行py文件例子:

In [ ]:%run lhr.py

加载了lhr.py文件,相当于导包。

In [ ]:%load lhr.py

把lhr.py的代码显示出来。

# %load app.py
from flask import Flask
from flask import render_template
from flask import request
from datetime import datetime
import re
import pymysql



app = Flask(__name__)

# @app.route("/")
# def home():
#     return 'Hello, Flask!'

# @app.route("/hello/<name>")
# def hello_there(name):
#     return render_template(
#         "hello_there.html",
#         name = name,
#         date = datetime.now()
#     )

# @app.route("/test2")
# def test2():
#     return render_template(
#         "test2.html"
#     )
 
# @app.route("/api/data")
# def get_data():
#     return app.send_static_file("data.json")

# Replace the existing home function with the one below
@app.route("/")
def home():
    return render_template("home.html")

# New functions
@app.route("/about")
def about():
    return render_template("about.html")

@app.route("/contact")
def contact():
    return render_template("contact.html")

@app.route("/test2")
def test2():
    return render_template(
        "test2.html"
    )

@app.route("/test3")
def test3():
    return render_template(
        "test3.html"
    )

@app.route("/test4")
def test4():
    return render_template(
        "test4.html"
    )

@app.route("/register")
def register():
    return render_template(
        "register.html"
    )

@app.route("/result",methods = ['POST','GET'])
def result():
    if request.method=='POST':
        result = request.form
#         print(result.get("name"))
#         print(type(result.get("name")))
#         print(result.get("gender"))
#         print(type(result.get("gender")))
#         print(result.get("age"))
#         print(type(result.get("age")))
#         print(result.get("level"))
#         print(type(result.get("level")))
        mname = result.get("name") 
        gender =  "男" if result.get("gender")==None else "女"
        age = result.get("age")
        level = result.get("level")
        print(mname,gender,age,level)
        print(f"insert into members values(null,{mname},{gender},{age},{level})")
        try:
            print(1)
            cursor.execute("use yacht_club")
            print(2)
            cursor.execute(f"insert into members values(null,'{mname}','{gender}',{age},{level})")
            print(3)
            conn.commit()
            print(4)
        except:
            print(5)
            return render_template(
                "home.html"
            )
        else:
            print(6)
            return render_template(
                "register.html"
#             "result.html",result = result
            )

if __name__ == '__main__':
    conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='20010529',charset = 'utf8')           
    cursor = conn.cursor()
    app.run(host="192.168.126.1",port=5000,debug=True)
    cursor.close()
    conn.close()
    

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐