Building a Data Science Website with Flask + Venv(Anaconda) — Part 1
I have recently undertaken to learn about building a website using Flask and Anaconda. I have chosen Anaconda since, for starters, I have worked extensively on it in my journey so far as a Data Scientist, and also to integrate Dashboards and Machine Learning models easily into the website.
In Part 1, we will see the basic set-up of git, virtual environment, Flask to start building an interactive data science and analytics website.
NOTE: This set-up is done for Windows. Kindly refer to the documentation of your respective OS to look-up alternate commands.
Set-up folder and git
- Create a folder
- Initialize git inside the folder
We will use the Anaconda prompt throughout this process since that makes your life easier. For initializing a folder and git, open up the Anaconda prompt and type the following commands
mkdir <your_folder_name> && cd <your_folder_name>git init
Set-up virtual environment using Anaconda
Creating a virtual environment is helpful since it protects your project/website from any unnecessary/inadvertent upgrades to any packages that occur when Anaconda is updated on your system. Follow the steps below to create a virtual environment inside your project folder.
Check the version of Anaconda (optional step)
conda -V
Update Anaconda to the latest version (optional step)
conda update conda
Create a virtual environment (might take some time).
conda create -n <envname>
Alternatively, if you want a specific version of python to be used for creating the virtual environment, simply use this command. Replace x.x with the python version that you desire. Eg. python=3.5
conda create -n <envname> python=x.x anaconda
Activate your virtual env
conda activate <envname>
Once the virtual environment is activated, the (base) written before the prompt will change to (<envname>)
Set-up Flask
Flask is usually installed and included in the Anaconda packages. If you do not find it, you can go through the following steps.
python -m pip install Flask
To quickly check if Flask is installed, run the following:
python>>> import flask
If no error is displayed, you have Flask installed and you can now exit python. Yay!
>>> exit()
Save your Requirements
Put the list of libraries/packages in your virtual environment into the requirements.txt file. This is a futuristic step which is required in case of any collaboration. Since we have set-up the virtual environment already, we can quickly get this step out of the way.
python -m pip freeze > requirements.txt
Write your first Flask program
Choose an IDE of your choice (I prefer PyCharm / VSCode) and create a file “app.py” in the folder that you set-up in the beginning.
Write the following snippet of code in your app.py file
from flask import Flask
app = Flask(__name__)
# Selects the page for which a function is to be defined. Right now there will only be one page in your website.
@app.route('/')
def hello():
return "<h1>Hello World!</h1>" \
"\nThis is my introduction to Flask!" \
"\nI can write a lot of things on this page.\nLet's get started!"
# The above function returns the HTML code to be displayed on the page
if __name__ == '__main__':
app.run()
Your Website is ready for launch!
Run the “app.py” file with the following command in the Anaconda prompt
python app.py
After you run the above command, you will get this output in your Anaconda prompt window. This means your website is up and running!
Have a Look!
Finally, to take a look at your website, type in either “http://127.0.0.1:5000/" or “localhost:5000” into your browser’s address bar, and voila! Your website is ready to go!
In the next parts, we will dive into more intricate functionality using Flask and also introduce UI components. Watch this space for future articles.
References:
- Flask by Example: https://realpython.com/flask-by-example-part-1-project-setup/
- Flask Web Development Tutorial #1: Installation and Setup on Windows: https://youtu.be/4mrq8PYQ0Zg
Thank you for reading.