0

the same .py script run itself works successfully, but in html get error

environment: open the live server on visual studio code to run .html

<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  </head>
 
<body>
  <b><p>this is the info from sql</p></b>
  <br>
 
  <py-config>
    packages = ["mysql"]

  </py-config>
  <py-script >

    import mysql.connector
    import webbrowser
    import time
    import pymysql

    mydb = mysql.connector.connect(
      host="196.168.101.141",
      user="root",
      password="password123", 
      database="cool_db",  
      auth_plugin='mysql_native_password'
    )
                  
    mycursor = mydb.cursor()
    mycursor.execute("SELECT P_TITLE,P_DESC  FROM webpage WHERE P_ID = 'en_1-01'")                       
                                            
    myresult = mycursor.fetchall()

    print(myresult)    

  </py-script>
   
</body>
</html>
  • error message
(PY1001): Unable to install package(s) 'mysql'. Reason: Can't find a pure Python 3 Wheel for package(s) 'mysql'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.

Question: Can PyScript (Pyodide + WASM + ...)in browser connects to the SQL server?

  1. If could, how can I correct it?
  2. If not, how can I do to achive the same goal
暫時時
  • 41
  • 4
  • I have done the research on web, but not seen the good solution for this case, therefore I need a hand, thanks. I run .py script can connect successd, and html run same .py script failed. Seems the html page don't get the defult function "mysql" to connect – 暫時時 Feb 01 '23 at 06:24
  • See the comment by John Hanley just below [the post entitled 'PyScript + Pyodide + MySQL DB connection = WebSockets problem, can't connect to my DB'](https://stackoverflow.com/q/74246324/8508004). – Wayne Feb 02 '23 at 18:30

1 Answers1

1

For Question #2:

See Datasette Lite - "Datasette running in your browser using WebAssembly and Pyodide"

Getting closer to what you seek, I see there's a 'Loading SQLite databases' section and 'Initializing with SQL' section. Also, I see that 'Live tool' version has a button 'Load SQL' that let's you "Enter a full URL to a SQL file".

Read more about it at:
Datasette Lite: a server-side Python web application running in a browser

Wayne
  • 6,607
  • 8
  • 36
  • 93