Giter Site home page Giter Site logo

muhammadmooazam / deploying-api-to-hosting-server_python_with-mysql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from muhammadraheelnaseem/deploying-testing-api-to-hosting-server

2.0 0.0 0.0 17 KB

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: MIT License

cloud database mysql python python-anywhere

deploying-api-to-hosting-server_python_with-mysql's Introduction

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! ๐Ÿš€

deploying-api-to-hosting-server_python_with-mysql's People

Contributors

muhammadmooazam avatar muhammadraheelnaseem avatar

Stargazers

Iqra Naz avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.