Spaces:
Running
Running
File size: 5,094 Bytes
e1c134c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#!/usr/bin/env python3
import subprocess
import os
import sys
import time
import pymysql
from sqlalchemy import create_engine
def check_mysql_installed():
"""Check if MySQL is installed"""
try:
if os.name == 'nt': # Windows
subprocess.run(['mysql', '--version'], check=True, stdout=subprocess.PIPE)
else: # Linux/Mac
subprocess.run(['which', 'mysql'], check=True, stdout=subprocess.PIPE)
return True
except (subprocess.SubprocessError, FileNotFoundError):
return False
def create_database_and_user():
"""Create database and user for transport application"""
try:
# Get MySQL root credentials
print("Please enter your MySQL root credentials to create the database and user:")
root_user = input("MySQL root username (usually 'root'): ") or 'root'
root_pass = input("MySQL root password: ")
# Connect to MySQL server
connection = pymysql.connect(
host='localhost',
user=root_user,
password=root_pass
)
try:
with connection.cursor() as cursor:
# Create database
cursor.execute("CREATE DATABASE IF NOT EXISTS transport_orders")
# Create user and grant privileges
cursor.execute("CREATE USER IF NOT EXISTS 'transport_user'@'localhost' IDENTIFIED BY 'transport_pass'")
cursor.execute("GRANT ALL PRIVILEGES ON transport_orders.* TO 'transport_user'@'localhost'")
cursor.execute("FLUSH PRIVILEGES")
print("\n✅ Database 'transport_orders' and user 'transport_user' created successfully.")
finally:
connection.close()
return True
except Exception as e:
print(f"\n❌ Error creating database: {e}")
return False
def test_connection():
"""Test connection to the database with transport_user"""
try:
# Try to connect with the application user
connection_string = 'mysql+pymysql://transport_user:transport_pass@localhost/transport_orders'
engine = create_engine(connection_string)
connection = engine.connect()
connection.close()
print("\n✅ Test connection successful with transport_user!")
print(f"\nConnection string: {connection_string}")
print("\nUse this connection string in your application.")
# Create environment variable for convenience
if os.name == 'nt': # Windows
print("\nTo set the database URL as an environment variable in Windows CMD:")
print("set DATABASE_URL=mysql+pymysql://transport_user:transport_pass@localhost/transport_orders")
print("\nOr in PowerShell:")
print("$env:DATABASE_URL=\"mysql+pymysql://transport_user:transport_pass@localhost/transport_orders\"")
else: # Linux/Mac
print("\nTo set the database URL as an environment variable:")
print("export DATABASE_URL=mysql+pymysql://transport_user:transport_pass@localhost/transport_orders")
return True
except Exception as e:
print(f"\n❌ Test connection failed: {e}")
return False
def install_requirements():
"""Install required Python packages"""
try:
print("\nInstalling required packages...")
subprocess.run([sys.executable, '-m', 'pip', 'install', 'pymysql', 'sqlalchemy', 'flask', 'flask-cors', 'streamlit'], check=True)
print("\n✅ Required packages installed successfully.")
return True
except subprocess.SubprocessError as e:
print(f"\n❌ Failed to install packages: {e}")
return False
def main():
"""Main function to setup MySQL for transport application"""
print("==== Transport Order Application - MySQL Setup ====\n")
# Check MySQL installation
print("Checking MySQL installation...")
if not check_mysql_installed():
print("\n❌ MySQL is not installed or not in PATH. Please install MySQL before continuing.")
print("Download MySQL from: https://dev.mysql.com/downloads/")
return
print("✅ MySQL is installed.")
# Install Python requirements
install_requirements()
# Create database and user
print("\n--- Setting up database and user ---")
if not create_database_and_user():
print("\nDatabase setup failed. Please check your MySQL installation and credentials.")
return
# Test connection with new user
print("\n--- Testing connection ---")
test_connection()
print("\n==== Setup Complete ====")
print("\nYou can now run the application:")
print("1. Start the backend: python backend.py")
print("2. Start the frontend: streamlit run frontend.py")
print("\nMake sure to run the backend first!")
if __name__ == "__main__":
main() |