Python

How to Transfer Python Flask Application from Localhost to Namecheap Shared hosting Server

Introduction:

How to transfer Python Flask Application from Localhost to Namecheap Shared Hosting Server-  Many people can create their Flask applications in VS Code but cannot deploy them on the live server. So, In this article, I will teach you how to transfer your Python Flask application from localhost to a live server. I am using Namecheap’s shared hosting server to deploy this simple Hello World project.




Buying NameCheap Shared Hosting:

As you know, NameCheap hosting is a well-known hosting provider that offers hosting servers at very affordable prices. So for application deployment, NameCheap hosting company is a great option. First, purchase a shared hosting plan from NameCheap, which is available at a very low price, only $2.98.

Python Flask Application

Create Flask Hello World Application on Localhost:

After purchasing the shared hosting plan, start creating your Flask application. When you create a Python Flask application, first create a folder and open it in your editor. I am using Visual Studio Code.

Python Flask Application

When you open your folder in Visual Code, you will see an interface like this.

Python Flask Application



When your project folder is successfully opened, make sure to install Flask before starting the application. To install Flask, simply type ‘pip install flask’ in the terminal and press enter.

Python Flask Application

I receive the ‘Requirement already satisfied’ message displayed on the terminal console because I have already installed it. However, if you have not installed it, you will see the installation process message.

Python Flask Application

When Python Flask is installed, it is then ready for actual coding.

Testing Flask Hello World App On Localhost:

To create a Python Flask application, you will need an App.py file and a templates folder. The templates folder will contain HTML and CSS related files, while the main application code will be in App.py

Python Flask Application

Now open app.py file and paste the below code

then open the index.html file from the templates folder and paste the below code



To run the Python Flask application, type ‘python app.py’ in the terminal and press enter.

Python Flask Application

When you press enter, you will see an output like this on the console. Copy the URL from the console and paste it into your browser.

Python Flask Application

When you paste the URL into your browser, you will see the output like this.

Python Flask Application


Transfer Python Flask Application to Live Server:

To transfer the Python Flask application to a live server, log in to your hosting account. once logged in, you will see 3 icons next to your domain: Home, drive, and SSL. To access the NameCheap hosting cPanel, hover your mouse cursor over the middle drive like icon. A dropdown menu will appear. Click on ‘Go to cPanel’ in that menu.

Python Flask Application

When the cPanel opens, click on ‘Setup Python App’.

Python Flask Application

To create a Python Flask application, now click on the ‘CREATE APPLICATION’ button.

Python Flask Application

First, select the recommended Python version.

Python Flask Application

Then set the name of your application’s root folder. This will be the folder where you create or transfer your Flask application. Next, set your application’s URL. After the URL, set the startup file name; in my case, I am using ‘app.py’. Then, set the application entry point; in my case, it is also ‘app’. When all these fields are filled, click the ‘Create’ button.

Python Flask Application




After creating the application, you will have an interface like this.

Python Flask Application

As you can see, my application folder ‘Hello_world’ has been created.

Python Flask Application

So, it’s now ready for creating or transferring the application. Now, open the ‘Hello_world’ folder and delete all the files. Then, compress your localhost’s application folder into a .zip file and upload it to the created application folder.

Python Flask Application

Select the helloworl.zip folder and press the extract button.

Python Flask Application

After extracting, you will have a folder created named ‘helloworld’.

Python Flask Application

Now, open the folder and move all the files in it to the domain root.

Python Flask Application

Now delete the helloworld and helloworld.zip folders

Python Flask Application



Flask Framework installation on live server:

When you want to run a Python Flask application on a live server, it’s essential to install the Flask framework. However, installing Flask on a live server is not like localhost. To install Python Flask on a live server, we create a .txt file and mention all the libraries we want to use in our Flask application in that file. So, in this article, I will install simple Flask because this is a very simple project and I don’t need any other libraries in this application. So, let’s create a lib.txt file and mention Flask in it and press the save button and close the lib.txt.

Python Flask Application

Now, to install it, open the application created in ‘setup python app’ and add a file with the same name lib.txt.

Python Flask Application

Once the file is added, click on ‘Run Pip install’ and select the file in which the flask framework is mentioned for installation.

Python Flask Application



When Flask is successfully installed, a popup message like this will appear.

Python Flask Application

Now start your python flask application

Python Flask Application

After starting the flask application click on open

Python Flask Application

As you can see our application successfully launched.

Python Flask Application

That is all about transferring the Python Flask application from localhost to the NameCheap live server. I hope this article will be very helpful for you. If you have any questions, you can ask me in the comments.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button