Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import pymysql | |
| def connect_db(config): | |
| db = pymysql.connect( | |
| host=config['host'], | |
| user=config['user'], | |
| passwd=config['passwd'], | |
| db=config['db'] | |
| ) | |
| cursor = db.cursor() | |
| return db, cursor | |
| def execute_sql(sql, db_config): | |
| db, cursor = connect_db(db_config) | |
| cursor.execute(sql) | |
| results = cursor.fetchall() | |
| return results | |
| def sql_helper(db_config, sql): | |
| results = execute_sql(sql, db_config) | |
| return results | |
| demo_db = { | |
| 'host': 'localhost', | |
| 'user': 'root', | |
| 'passwd': '123456', | |
| 'db': 'test' | |
| } | |
| iface = gr.Interface( | |
| fn=sql_helper, | |
| inputs=[ | |
| gr.inputs.Textbox(default=demo_db, label="Database Config"), | |
| gr.inputs.Textbox(lines=5, label="SQL Statement") | |
| ], | |
| outputs="table" | |
| ) | |
| iface.launch() |