https://python-guide-es.readthedocs.io/es/guide-es/dev/virtualenvs.html
Create an environment
ednalan@Cairocoders myapp % pip install virtualenv
ednalan@Cairocoders myapp % pip install virtualenv
Activate the environment
ednalan@Cairocoders myapp % source venv/bin/activate
(venv) ednalan@Cairocoders myapp %
Install Flask
https://pypi.org/project/Flask/
(venv) ednalan@Cairocoders myapp % pip install -U Flask
(venv) ednalan@Cairocoders myapp % flask run
Install requirements
pip install -U flask-cors
https://pypi.org/project/Flask-Cors/
Flask-SQLAlchemy
Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application.
https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/
(venv) PS C:\flask_dev\myapp>pip install -U Flask-SQLAlchemy
Flask + marshmallow for beautiful APIs
https://pypi.org/project/flask-marshmallow/
(venv) PS C:\flask_dev\myapp>pip install flask-marshmallow
python3 -m pip install
https://pypi.org/project/pymysql/
app.py
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 | / / app.py from flask import Flask, jsonify, request from flask_marshmallow import Marshmallow #ModuleNotFoundError: No module named 'flask_marshmallow' = pip install flask-marshmallow https://pypi.org/project/flask- from flask_cors import CORS, cross_origin #ModuleNotFoundError: No module named 'flask_cors' = pip install Flask-Cors from models import db, Users app = Flask(__name__) app.config[ 'SECRET_KEY' ] = 'cairocoders-ednalan' #app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///flaskdb.db' # Databse configuration mysql Username:password@hostname/databasename app.config[ 'SQLALCHEMY_DATABASE_URI' ] = 'mysql+pymysql://root:root@localhost:8889/flasknextjs' #python3 -m pip install PyMySQL https://pypi.org/project/pymysql/ SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_ECHO = True CORS(app, supports_credentials = True ) db.init_app(app) with app.app_context(): db.create_all() ma = Marshmallow(app) class UserSchema(ma.Schema): class Meta: fields = ( 'id' , 'name' , 'email' , 'password' ) user_schema = UserSchema() users_schema = UserSchema(many = True ) @app .route( "/" ) def hello_world(): return "<p>Hello, World!=</p>" @app .route( '/users' , methods = [ 'GET' ]) def listusers(): all_users = Users.query. all () results = users_schema.dump(all_users) return jsonify(results) @app .route( '/userdetails/<id>' ,methods = [ 'GET' ]) def userdetails( id ): user = Users.query.get( id ) return user_schema.jsonify(user) @app .route( '/userupdate/<id>' ,methods = [ 'PUT' ]) def userupdate( id ): user = Users.query.get( id ) name = request.json[ 'name' ] email = request.json[ 'email' ] user.name = name user.email = email db.session.commit() return user_schema.jsonify(user) @app .route( '/userdelete/<id>' ,methods = [ 'DELETE' ]) def userdelete( id ): user = Users.query.get( id ) db.session.delete(user) db.session.commit() return user_schema.jsonify(user) @app .route( '/newuser' ,methods = [ 'POST' ]) def newuser(): name = request.json[ 'name' ] email = request.json[ 'email' ] password = request.json[ 'password' ] print (name) print (email) print (password) users = Users(name = name, email = email, password = password) db.session.add(users) db.session.commit() return user_schema.jsonify(users) if __name__ = = "__main__" : app.run(debug = True ) |
1 2 3 4 5 6 7 8 9 10 11 | / / models.py from flask_sqlalchemy import SQLAlchemy #pip install -U Flask-SQLAlchemy db = SQLAlchemy() class Users(db.Model): __tablename__ = "tblusers" id = db.Column(db.Integer, primary_key = True ) name = db.Column(db.String( 150 ), index = True ) email = db.Column(db.String( 150 ), index = True , unique = True ) password = db.Column(db.String( 255 ), index = True ) |
Next.js Install requirements
npm install axios
https://www.npmjs.com/package/axios
app\page.tsx
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 | //app\page.tsx import Link from "next/link" ; import TableData from "@/components/tabledata" ; import { Suspense } from "react" ; import { Spinner } from "@/components/spinner" ; export default function Home() { return ( <div className= "w-screen py-20 flex justify-center flex-col items-center" > <div className= "flex items-center justify-between gap-1 mb-5" > <h1 className= "text-4xl font-bold" >Next.js 14 Python Flask CRUD Mysql (Create Read Update and Delete )| TailwindCSS DaisyUI</h1> </div> <div className= "overflow-x-auto" > <div className= "mb-2 w-full text-right" > <Link href= "/user/create" className= "btn btn-primary" > Create </Link> </div> <Suspense fallback={<Spinner />}> <TableData/> </Suspense> </div> </div> ); } |
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 | //components\tabledata.tsx "use client" ; import React, { useEffect, useState } from "react" ; import Link from "next/link" ; export default function Users() { const [userData, setUSerData] = useState([]); useEffect(() => { fetchData(); }, []) const fetchData = async () => { try { console.log(result.data); setUSerData(result.data) } catch (err) { console.log( "somthing Wrong" ); } } const handleDelete=async(id)=>{ console.log(id); const newUserData=userData.filter((item)=>{ return ( item.id !==id ) }) setUSerData(newUserData); } return ( <table className= "table table-zebra" > <thead className= "text-sm text-gray-700 uppercase bg-gray-50" > <tr> <th className= "py-3 px-6" >#</th> <th className= "py-3 px-6" >Name</th> <th className= "py-3 px-6" >Email</th> <th className= "py-3 px-6" >Password</th> <th className= "py-3 px-6 text-center" >Actions</th> </tr> </thead> <tbody> {userData.map((rs, index) => ( <tr key={rs.id} className= "bg-white border-b" > <td className= "py-3 px-6" >{index + 1}</td> <td className= "py-3 px-6" >{rs.name}</td> <td className= "py-3 px-6" >{rs.email}</td> <td className= "py-3 px-6" >{rs.password}</td> <td className= "flex justify-center gap-1 py-3" > <Link href={`/user/view/${rs.id}`} className= "btn btn-info" > View </Link> <Link href={`/user/edit/${rs.id}`} className= "btn btn-primary" > Edit </Link> <button onClick={()=>handleDelete(rs.id)} className= "btn btn-secondary" > Delete </button> </td> </tr> ))} </tbody> </table> ); } |
1 2 3 4 5 6 | //components\spinner.tsx export const Spinner = () => { return ( <span className= "loading loading-spinner loading-lg" ></span> ); }; |
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 | //app\user\create\page.tsx "use client" ; import React, { useState } from "react" ; const CreateUserPage = () => { const [userField, setUserField] = useState({ name: "" , email: "" , password: "" }); const changeUserFieldHandler = (e) => { setUserField({ ...userField, [e.target.name]: e.target.value }); //console.log(userField); } const onSubmitChange = async (e) => { e.preventDefault(); try { console.log(responce) window.location.href = '/' ; } catch (err) { console.log( "Something Wrong" ); } } return ( <div className= "max-w-md mx-auto mt-5" > <h1 className= "text-2xl text-center mb-2" >Add New User</h1> <div> <form> <div className= "mb-5" > <label htmlFor= "name" className= "block text-sm font-medium text-gray-900" > Full Name </label> <input type= "text" name= "name" id= "name" className= "input input-bordered input-primary w-full max-w-xs" placeholder= "Full Name..." onChange={e => changeUserFieldHandler(e)} /> </div> <div className= "mb-5" > <label htmlFor= "email" className= "block text-sm font-medium text-gray-900" > Email </label> <input type= "email" name= "email" id= "email" className= "input input-bordered input-primary w-full max-w-xs" placeholder= "Email..." onChange={e => changeUserFieldHandler(e)} /> </div> <div className= "mb-5" > <label htmlFor= "password" className= "block text-sm font-medium text-gray-900" > Password </label> <input type= "text" name= "password" id= "password" className= "input input-bordered input-primary w-full max-w-xs" placeholder= "Password..." onChange={e => changeUserFieldHandler(e)} /> </div> <button type= "submit" className= "btn btn-primary" onClick={e => onSubmitChange(e)}>Add User</button> </form> </div> </div> ); }; export default CreateUserPage; |
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 | //app\user\edit\[id]\page.tsx "use client" ; import React, { useState, useEffect } from 'react' ; import Link from "next/link" ; import { useParams } from 'next/navigation' export default function ViewUserPage() { const {id}=useParams(); console.log(id); const [userField, setUserField] = useState({ name: "" , email: "" }); useEffect(()=>{ fetchUser(); },[id]); const fetchUser=async()=>{ try { console.log(result.data); setUserField(result.data) } catch (err){ console.log( "Something Wrong" ); } } const changeUserFieldHandler = (e) => { setUserField({ ...userField, [e.target.name]: e.target.value }); console.log(userField); } const onSubmitChange = async (e) => { e.preventDefault(); try { window.location.href = '/' ; } catch (err) { console.log( "Something Wrong" ); } } return ( <div className= "max-w-md mx-auto mt-5" > <h1 className= "text-2xl text-center mb-2" >Edit Form</h1> <form> <div className= "mb-3 mt-3" > <label className= "block text-sm font-medium text-gray-900" > ID:</label> <input type= "text" id= "id" name= "id" value={id} disabled /> </div> <div className= "mb-3 mt-3" > <label className= "block text-sm font-medium text-gray-900" > Full Name:</label> <input type= "text" className= "input input-bordered input-primary w-full max-w-xs" placeholder= "Enter Your Full Name" name= "name" value={userField.name} onChange={e => changeUserFieldHandler(e)} /> </div> <div className= "mb-3 mt-3" > <label className= "block text-sm font-medium text-gray-900" >Email:</label> <input type= "email" className= "input input-bordered input-primary w-full max-w-xs" id= "email" placeholder= "Enter email" name= "email" value={userField.email} onChange={e => changeUserFieldHandler(e)}/> </div> <div className= "mb-3 mt-3" > <label className= "block text-sm font-medium text-gray-900" >Password:</label> <input type= "text" className= "input input-bordered input-primary w-full max-w-xs" id= "password" placeholder= "Enter password" name= "password" onChange={e => changeUserFieldHandler(e)} required/> </div> <button type= "submit" className= "btn btn-primary" onClick={e=>onSubmitChange(e)}>Update</button> </form> </div> ); } |
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 | //app\user\view\[id]\page.tsx "use client" ; import React, { useState, useEffect } from 'react' ; import Link from "next/link" ; import { useParams } from 'next/navigation' export default function ViewUserPage() { const {id}=useParams(); console.log(id); const [user,setUser]=useState([]); useEffect(()=>{ fetchUser(); },[id]); const fetchUser=async()=>{ try { console.log(result.data); setUser(result.data) } catch (err){ console.log( "Something Wrong" ); } } return ( <div className= "max-w-md mx-auto mt-5" > <h1 className= "text-2xl text-center mb-2" >View User</h1> <table className= "table table-zebra" > <thead className= "text-sm text-gray-700 uppercase bg-gray-50" > <tr> <th>S No.</th> <th>Full Name</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>{user.id}</td> <td>{user.name}</td> <td>{user.email}</td> </tr> </tbody> </table> </div> ); } |
Github - Next.js 14 Python Flask CRUD Mysql (Create Read Update and Delete)| TailwindCSS DaisyUI