前言:为什么选择RockyLinux
昨天在Centos7.9上实验安装了streamlit,并成功运行了first_app.py程序,但是后面想要连接数据库时却一直不成功,代码是用的人家写的100行那个,一直提示ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+,currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips 26 Jan 2017' 错误,意思就是python运行代码需要openssl版本大于1.11而本地是1.0.2k,于是我就编译安装了OpenSSL1.1.1u版本,结果升级成功之后那个openssl-libs一直是1.0.2k,无论怎么弄都不行,如下
[root@localhost Python-3.10.12]# python -c "import ssl; print(ssl.OPENSSL_VERSION)" OpenSSL 1.0.2k-fips 26 Jan 2017
弄了好几个小时,ChatGPT都被我问麻了各种百度谷歌也没搞定,最后转centos8,结果这货太久没用了,yum源都没了,然后python也是3.6不符合要求,干脆转投Ubuntu。一口气装了20、22、23三个版本,众所周知Ubuntu最大的优点就是软件新,OpenSSL和python版本都没话说,也可以运行streamlit,但是有个麻烦事绕不过,python必须运行在虚拟环境中,因为据说怕损坏系统,这就很麻烦了每次要先source进虚拟环境完了deactivate退出,虽然我玩树莓派对于debian系列命令并不陌生,但是每次sudo也很麻烦。此时突然想到rockylinux,虽然在它刚出来时曾下载尝鲜了下但后面就没用过了。于是又下载了最新的8.8和9.2版本安装,一番折腾感觉rockylinux9.2是最适合的,理由如下:
1、保持RHEL系列命令的习惯,不用换山头转投Ubuntu每次敲命令先sudo,虽说Ubuntu也可以root运行,但是这样太小众了自己也不放心,而且Ubuntu的标志之一就是sudo
2、软件新,因为streamlit本身也是个新玩意,对于旧的系统极度不友好,9.2系统内核5.14,opensll 3.0版本,python默认3.9,python2版本直接舍弃了。够狠,不用重新编译安装python,rockylinux8.8虽然OpenSSL1.1但是python版本还是3.6太低了,streamlit最低要求3.8,还得重新编译升级python
3、streamlit跑python不用进虚拟环境,写完直接跑,至于搞坏系统什么的这年头谁还不是先装个虚拟机然后搞个快照,坏了就恢复下快照,难不成真有人在服务器上直接做测试不成。
软件版本如下
[root@localhost ~]# cat /etc/redhat-release Rocky Linux release 9.2 (Blue Onyx) [root@localhost ~]# uname -r 5.14.0-284.11.1.el9_2.x86_64 [root@localhost ~]# openssl version OpenSSL 3.0.7 1 Nov 2022 (Library: OpenSSL 3.0.7 1 Nov 2022) [root@localhost ~]# python --version Python 3.9.16
正式安装streamlit环境
前面说了python是新版,OpenSSL是新版这下就省事了
pip install streamlit streamlit hello systemctl stop firewalld streamlit hello --server.port 80
此时通过浏览器访问rockylinux的IP地址应该有如下显示了
安装并配置好MySQL数据库
RockyLinux9.2系统自带的8.0版本数据库,直接yum安装,你也可以dnf,效果一样
yum install mysql-server -y pip install pymysql systemctl enable mysqld --now mysql -u root -p
安装完成不需要密码,直接回车进去设置个密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; exit;
最后这个password就是你数据库的root密码
Python连接MySQL数据库并将表显示在Web网页上
先写个mysql.py,代码用的别人写好的
[root@localhost ~]# cat mysql.py
import streamlit as st import pymysql import pandas as pd import os col_a, col_b, col_c = st.columns([1, 3, 1]) with col_b: st.header('超级数据库管理系统 v1.0') # 抽取全局参数 all_table = pd.Series(['default_table']) table = 'default' conn = '' btn_flag = True print('===============') # 页面布局第一行两列 col1, col_block1, col2 = st.columns([1, 1, 3]) with col1: st.info('连接信息') with col2: st.error('SQL查询窗口(暂未开发,等一等!)') st.header('') # 页面布局第二行四列 col20, col_block, col21, col22, col23 = st.columns([2, 1, 2, 1, 2]) with col20: db_type = st.selectbox( '数据库类型', ['MySQL', 'Postgres', 'Hive'] ) with st.expander('数据库参数'): host_name = st.text_input('Host', value='localhost') port = st.text_input('Port', value=3306) user_name = st.text_input('User', value='root') user_pass = st.text_input('Password', value='root') db_name = st.text_input('db', value='test') btn_flag = st.button('点击连接') if btn_flag: st.balloons() print('========数据库连接部分========') conn = pymysql.connect( host=host_name, user=user_name, password=user_pass, port=int(port), db=db_name, charset='utf8' ) db_info = [host_name, port, user_name, user_pass, db_name] db_info_series = pd.Series(db_info) db_info_series.to_csv('db_info_series.csv') print(f'save db_info to csv success!') tables_sql = 'show tables' tables = pd.read_sql(tables_sql, conn) all_table = tables[tables.columns.values[0]] table = all_table[0] all_table.to_csv('all_table.csv') print(f'save all_table to csv success!') with col21: st.success(f'表列表显示') print(f'======表下拉框部分========') if os.path.exists('all_table.csv'): c_tables = pd.read_csv('all_table.csv') all_table = c_tables[c_tables.columns.values[1]] table = all_table[0] if table in all_table.values: table = st.selectbox( '', all_table ) with st.expander(f'查看{table}元数据'): print(f'======元数据部分======') print(f'current_table:{table}') print(f'all_table:{all_table.values}') print(f'conn:{conn}') if os.path.exists('db_info_series.csv'): c_db_info = pd.read_csv('db_info_series.csv').values conn = pymysql.connect( host=c_db_info[0][1], user=c_db_info[2][1], password=c_db_info[3][1], port=int(c_db_info[1][1]), db=c_db_info[4][1], charset='utf8' ) print(f'conn:{conn}') if table in all_table.values: schema_sql = f'desc {table}' schema_df = pd.read_sql(schema_sql, conn) st.table(schema_df[['Field', 'Type']]) with col23: st.success(f'{table}数据') print(f'======数据展示部分======') print(f'current_table:{table}') print(f'all_table:{all_table.values}') if table in all_table.values: query_sql = f'select * from {table}' query_df = pd.read_sql(query_sql, conn) st.table(query_df)
最终效果展示
此时用streamlit运行这个代码程序
streamlit run mysql.py --server.port 80
左边框里输入你真实的mysql连接参数,然后点击连接,结果就出来了,如下(非全图,只截了一部分):
发表评论