Skip to content

Learn API development with GET, POST, PUT, DELETE methods on PythonAnywhere. Build powerful APIs for data manipulation and explore routing, request handling, data validation, error handling, and authentication. Perfect for developers deploying RESTful APIs on PythonAnywhere.

License

Notifications You must be signed in to change notification settings

MuhammadMooazam/Deploying-API-To-Hosting-Server_Python_with-MySQL

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Deploy FLASK API To PythonAnywhere Cloud Hosting Server

PythonAnywhere Logo

Welcome to PythonAnywhere: Your Gateway to Python in the Cloud! πŸš€

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!


Explore PythonAnywhere πŸ“

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.

Why PythonAnywhere Rocks πŸ’Ž

PythonAnywhere is the perfect playground for Python web development projects, supporting a myriad of web technologies and frameworks.


Start Your Python Journey Here 🌐

Ready to Dive In?

  1. Sign Up: Create your PythonAnywhere account at pythonanywhere.com.
  2. Explore: Dive into web-based code editors, the Python console, and other features to unleash your Python prowess.
  3. Deploy: Launch your Python applications with ease – embrace cloud-based hosting.
  4. Scale Up: Focus on building and scaling your projects while leaving server management behind.

Follow the Road πŸ›€οΈ

PythonAnywhere Interface

PythonAnywhere Features

PythonAnywhere Database Support

PythonAnywhere File System

PythonAnywhere Community

PythonAnywhere Python Console

PythonAnywhere Databases

PythonAnywhere Task Scheduler

PythonAnywhere Code Editor

PythonAnywhere Project Management

PythonAnywhere Python Packages


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()

image

image

image

image

image

image

image

image

image

image

image

image

Join Our Vibrant Community! 🌟

Explore the PythonAnywhere community, where Python aficionados unite to share knowledge, solve problems, and celebrate Python's awesomeness together!


License πŸ“œ

This project is licensed under the MIT License.

Are you ready to kickstart your Python journey with PythonAnywhere? Unleash the magic of Python today! πŸš€

About

Learn API development with GET, POST, PUT, DELETE methods on PythonAnywhere. Build powerful APIs for data manipulation and explore routing, request handling, data validation, error handling, and authentication. Perfect for developers deploying RESTful APIs on PythonAnywhere.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published