Best Flask open-source libraries and packages

Deploying Testing API To Hosting Server

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.
Updated 8 months ago

Deploying-Testing-API-To-Hosting-Server

Now Deploying API script into pythonanywhere.com

What is pythonanywhere.com?

PythonAnywhere is a web hosting platform that allows you to run your Python applications and websites in the cloud. It provides a fully integrated environment for Python development, deployment, and web hosting, eliminating the need for managing servers and infrastructure.

With PythonAnywhere, you can easily deploy and host Python applications, including web applications built with frameworks like Flask and Django. It offers features such as web-based code editors, a Python console, scheduled tasks, and support for various Python versions and libraries.

PythonAnywhere also provides access to databases like MySQL, PostgreSQL, and SQLite, allowing you to store and retrieve data for your applications. It offers a web-based file system for managing your project files and allows you to install additional Python packages using the package manager.

The platform supports various web technologies and frameworks, making it suitable for a wide range of Python web development projects. PythonAnywhere offers free accounts with limited resources, as well as paid plans with additional features and higher resource allocations.

Overall, PythonAnywhere simplifies the process of hosting and deploying Python applications, allowing developers to focus on building and scaling their projects without the hassle of managing infrastructure.

image image image image image image image image image image image image image image image image image image image image image image image image image image image

Paste this API script into flasK_app.py and don't forgot to comment app.run() in 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