Ready to elevate your Python projects to the next level? Say hello to PythonAnywhere, the ultimate destination for seamless Python development, deployment, and web hosting. Wave goodbye to server management headaches and say hello to an integrated Python paradise!
PythonAnywhere is not just a hosting platform; it's your launchpad to the Python cosmos. Here's why it's extraordinary:
- Effortless Deployment: Easily deploy Python applications, including dazzling web apps built with Flask, Django, and more.
- Web-Based Code Editors: Edit Python code directly in your browser for quick changes and enhancements.
- Python Console: Get instant access to a Python console for experimentation and debugging.
- Scheduled Tasks: Automate Python tasks with scheduled executions, letting your scripts work their magic while you rest.
- Database Magic: Connect seamlessly to databases like MySQL, PostgreSQL, and SQLite for effortless data storage and retrieval.
- Package Wonderland: Expand your Python toolkit by installing additional packages using the built-in package manager.
PythonAnywhere is the perfect playground for Python web development projects, supporting a myriad of web technologies and frameworks.
Ready to Dive In?
- Sign Up: Create your PythonAnywhere account at pythonanywhere.com.
- Explore: Dive into web-based code editors, the Python console, and other features to unleash your Python prowess.
- Deploy: Launch your Python applications with ease โ embrace cloud-based hosting.
- Scale Up: Focus on building and scaling your projects while leaving server management behind.
Paste this API script into the flask_app.py file, change the database connections with your values, and don't forget to comment app.run() in the last line of the script
from flask import Flask, request, jsonify
import mysql.connector
app = Flask(__name__)
# MySQL configurations
db = mysql.connector.connect(
host='[your_mysql_host]',
user='[your_mysql_user]',
password='[your_mysql_password]',
database='[your_mysql_database]'
)
# Create a table in the database if it doesn't exist
def create_table():
cur = db.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS temp_emp_3 (ID INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(100), Email VARCHAR(100), Address VARCHAR(100))")
db.commit()
cur.close()
# API route to get all users
@app.route('/api/users', methods=['GET'])
def get_users():
cur = db.cursor()
cur.execute("SELECT * FROM temp_emp_3")
users = cur.fetchall()
cur.close()
user_list = []
for user in users:
user_dict = {
'id': user[0],
'name': user[1],
'email': user[2],
'address': user[3]
}
user_list.append(user_dict)
return jsonify(user_list)
# API route to create a new user
@app.route('/api/users/add', methods=['POST'])
def create_user():
name = request.json.get('name')
email = request.json.get('email')
addr = request.json.get('address')
cur = db.cursor()
cur.execute("INSERT INTO temp_emp_3 (Name, Email, Address) VALUES (%s, %s, %s)", (name, email, addr))
db.commit()
cur.close()
return jsonify({'message': 'User created successfully'})
# API route to update an existing user
@app.route('/api/users/update/<string:user_id>', methods=['PUT'])
def update_user(user_id):
name = request.json.get('name')
email = request.json.get('email')
addr = request.json.get('address')
cur = db.cursor()
cur.execute("UPDATE temp_emp_3 SET Name = %s, Email = %s, Address = %s WHERE ID = %s", (name, email, addr, user_id))
db.commit()
cur.close()
return jsonify({'message': 'User updated successfully'})
# API route to delete a user
@app.route('/api/users/delete/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
cur = db.cursor()
cur.execute("DELETE FROM temp_emp_3 WHERE ID = %s", (user_id,))
db.commit()
cur.close()
return jsonify({'message': 'User deleted successfully'})
if __name__ == '__main__':
create_table()
# app.run()
Explore the PythonAnywhere community, where Python aficionados unite to share knowledge, solve problems, and celebrate Python's awesomeness together!
This project is licensed under the MIT License.
Are you ready to kickstart your Python journey with PythonAnywhere? Unleash the magic of Python today! ๐