Xampp Python Flask

How to run flask socket.io on localhost (xampp). As a side note, Python 3 is also currently out, as gevent only runs on Python 2 (through I think there's going to be good news about that soon, working on some ideas to get socketio running on Python 3 right now). Then reactivate your venv and continue. Now let's install Flask: pip install flask. If everything goes well, you'll get a success message at the end. Now let's create a simple Python script that will serve as our root Flask app. I called it piapp.py but you can call it anything you want. Sudo nano piapp.py. In the file, add this.

Flask is a fantastic micro web framework for Python, however, it is not a native web language. So to get our Python code running on a web server is tricky. Apache will use WSGI file to access our Flask application, so the WSGI file allows Apache to interact with Python as if it is native. It's a simple script. Import pymysql from flask import Flask, rendertemplate, session, redirect, urlfor from flasksession import Session from sqlalchemy import createengine from sqlalchemy.orm import scopedsession. The official dedicated python forum. I'm trying to build a webapp using Flask and the part with the database is a little bit confunsing since and want to wrap the creation of the tables in my main app file.

  • Flask Tutorial
  • Flask Useful Resources
  • Selected Reading

Using raw SQL in Flask web applications to perform CRUD operations on database can be tedious. Instead, SQLAlchemy, a Python toolkit is a powerful OR Mapper that gives application developers the full power and flexibility of SQL. Flask-SQLAlchemy is the Flask extension that adds support for SQLAlchemy to your Flask application.

What is ORM (Object Relation Mapping)?

Most programming language platforms are object oriented. Data in RDBMS servers on the other hand is stored as tables. Object relation mapping is a technique of mapping object parameters to the underlying RDBMS table structure. An ORM API provides methods to perform CRUD operations without having to write raw SQL statements.

In this section, we are going to study the ORM techniques of Flask-SQLAlchemy and build a small web application.

Step 1 − Install Flask-SQLAlchemy extension.

Step 2 − You need to import SQLAlchemy class from this module.

Step 3 − Now create a Flask application object and set URI for the database to be used.

Step 4 − Then create an object of SQLAlchemy class with application object as the parameter. This object contains helper functions for ORM operations. It also provides a parent Model class using which user defined models are declared. In the snippet below, a students model is created.

Step 5 − To create / use database mentioned in URI, run the create_all() method.

The Session object of SQLAlchemy manages all persistence operations of ORM object.

The following session methods perform CRUD operations −

  • db.session.add(model object) − inserts a record into mapped table

  • db.session.delete(model object) − deletes record from table

  • model.query.all() − retrieves all records from table (corresponding to SELECT query).

You can apply a filter to the retrieved record set by using the filter attribute. For instance, in order to retrieve records with city = ’Hyderabad’ in students table, use following statement −

With this much of background, now we shall provide view functions for our application to add a student data.

The entry point of the application is show_all() function bound to ‘/’ URL. The Record set of students table is sent as parameter to the HTML template. The Server side code in the template renders the records in HTML table form.


The HTML script of the template (‘show_all.html’) is like this −

The above page contains a hyperlink to ‘/new’ URL mapping new() function. When clicked, it opens a Student Information form. The data is posted to the same URL in POST method.


Xampp Python Flask

When the http method is detected as POST, the form data is added in the students table and the application returns to homepage showing the added data.

Given below is the complete code of application (app.py).

Run the script from Python shell and enter http://localhost:5000/ in the browser.

Click the ‘Add Student’ link to open Student information form.

Fill the form and submit. The home page reappears with the submitted data.

We can see the output as shown below.


In this post we will implement Python flask REST API MongoDB CRUD example, where CRUD means Create, Read, Update, and Delete operations. So we will perform these CRUD operations on MongoDB. We will create REST or RESTful API using Flask in Python. We need to install the required module Flask-PyMongo for connecting to MongoDB using Flask in Python.


Have Python 3.6.5 installed in Windows (or Unix)

Example and Source Code

We do not have front-end or User Interface (UI) here. We are building REST or RESTful web services which could be consumed by any consumer. These services are decoupled from consumer. You can use any UI technologies, such as, ReactJS, AngularJS or even jQuery, HTML to showcase your data for your users.

Preparing your workspace

Preparing your workspace is one of the first things that you can do to make sure that you start off well. The first step is to check your working directory.

When you are working in the Python terminal, you need first navigate to the directory, where your file is located and then start up Python, i.e., you have to make sure that your file is located in the directory where you want to work from.

For this Python Flask REST API MongoDB CRUD Example, we need modules, such as, flask and flask-pymongo. The module flask works as a web framework and flask-pymongo module is required to establish connection with MongoDB database and query the database using flask in Python programming language.

We will create a project directory flask-rest-mongodb in any physical location of your disk drive.

In subsequent section I may not speak about this project root directory and we will assume that we are talking relative to the project root directory.

Configuring Flask and MongoDB

Create the below app.py script(py is the extension to indicate Python script) where we import the flask module. This file should be created under the project root directory. Notice how we create flask instance.

We have also configured MongoDB database with Flask and we are using here roytuts database.

As we mentioned Installing MongoDB in Windows in the prerequisites section, so you must go through this tutorial to install MongoDB server and create database roytuts.

Xampp Python Flask Online

Creating REST Endpoints

Now we will create REST endpoints for performing CRUD operations.

We will create main.py script under the project root directory. This script is the perfect instance of Python Flask REST API MongoDB CRUD Example. It defines all REST URIs for performing CRUD operations. It will also query MongoDB database server to read, insert, update and delete.

Here, we use http GET, POST, PUT method and DELETE methods for fetching, creating, updating and deleting user from or to MongoDB, respectively.

I have defined only 404 method to handle not found error. You should basically handle required errors, such as, server errors for http responses 500, occurred during the REST API calls.

Xampp Python Flask Example

We convert BSON to JSON using bson.json_util module to avoid serialization error.

We are updating record to or deleting record from MongoDB server using _id value.

The full source code is given below:

Related Posts:

Running the Application

Now we are done with coding, so we will run our application by executing the Python script main.py.

Once you execute the main.py using command python main.py, then you will see below output in the cmd console as shown in the below image.

Testing the Application

We will use REST client here to test our application.

Creating User

Reading User

Reading All

Request Method – GET

Request URL – http://localhost:5000/users


Reading Single

Request Method – GET

Request URL – http://localhost:5000/5d1f12a04ecb7854a4b347a4


Updating User

Request Method – PUT

Request URL – http://localhost:5000/update

Request Body

Jiffy lube oil change cost 2020. Response – 'User updated successfully!'


Deleting User

Request Method – DELETE

Request URL – http://localhost:5000/delete/5d1f12a04ecb7854a4b347a4

Response – 'User deleted successfully!'

That’s all. Hope you got an idea on Python Flask REST API MongoDB CRUD Example.

You may also like to read Spring Boot MongoDB CRUD Example.

Xampp python flask exampleFlask

Source Code

Thanks for reading.