FreeBSD Flask - Complete Guide Installation configuration and use

Entering the world of web development, there are so many languages, frameworks, and technologies that need to be mastered. However, what if there was a tool that could make your journey easier and help you build advanced web applications? That tool is Python Flask, a lightweight and intuitive web framework.

Through its minimalist yet feature-packed design, Flask allows you to build simple static websites to complex web applications with high speed and efficiency. What exactly is Python Flask? Why do so many developers, both beginners and experts, utilize it as their tool of choice? How can you use it to create your own applications?

Python Flask is a light and simple web framework written in the Python programming language. Flask is considered lightweight because it does not demand much from the system or require many dependencies compared to other Python frameworks. In other words, Flask gives its users more flexibility and control to build web applications according to their individual needs and desires.

Using Python Flask, you can create faster and more efficient web apps. This framework can be used for small, medium projects, and more complex web applications.

This article will discuss the installation process, configuration and how to use Python Flask on FreeBSD.






1. Install Flask

Applications supported by the Python language on FreeBSD are very complete. We can see this with the repository support provided by FreeBSD. Flask applications written in Python are also available in the default FreeBSD package.

In FreeBSD, you can install Flask easily. There are two ways to install Flask, namely with the PKG package or system ports and via the Python virtual environment (venv). Between the two methods, we prefer the first method. Apart from being easy, almost all dependencies are available and you can easily install them.

If you use Python venv, many versions are often out of sync with the FreeBSD version. Apart from that, Python Venv also often experiences kernel problems. So many Python applications cannot run on Venv. This is clearly different from Linux, where it is very easy to install Python packages with venv.

As an example, in this post we will install Flask with the FreeBSD PKG package. The guide below will make it easier for you to install Flask on FreeBSD.

Install dependencies
root@ns3:~ # pkg install py39-flit-core py39-build py39-installer py39-pytest py39-werkzeug py39-Jinja2 py39-itsdangerous bash
Install Flask
root@ns3:~ # pkg install py39-flask

2. How to run Flask

On FreeBSD systems, there are many ways to run Flask. In this article we will demonstrate the general method that FreeBSD users usually use, namely by Bash and downloading the Python application package from the Github repository.

a. Run Flask with Bash

In this section, we will demonstrate how to run Flash with Bash. To make it easier for you to use Flask, create a working directory to place Flask files.

Create a Flask directory
root@ns3:~ # mkdir -p /usr/local/etc/flask
root@ns3:~ # cd /usr/local/etc/flask
root@ns3:/usr/local/etc/flask #
After that, in the flask directory, you create a python file "flaskApp.py", and type the script below in that file.

Create the file "flaskApp.py"
root@ns3:/usr/local/etc/flask # touch flaskApp.py
root@ns3:/usr/local/etc/flask # ee flaskApp.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return '"FreeBSD The Power To Server"'

if __name__ == '__main__':
    app.run()
Then you can run Flask with Bash, here is an example.

Run Flask With Bash
root@ns3:/usr/local/etc/flask # bash
[root@ns3 /usr/local/etc/flask]# export FLASK_APP=flaskApp.py
[root@ns3 /usr/local/etc/flask]# flask run --host=192.168.5.2 --debug
 * Serving Flask app 'flaskApp.py'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://192.168.5.2:5000
Press CTRL+C to quit
You can see the results by opening Google Chrome and typing "http://192.168.5.2:5000/". If you successfully run Flask, Google Chrome will display the words "FreeBSD The Power To Server". 192.168.5.2 is the private IP of our FreeBSD server, you can adjust it to your FreeBSD private IP.

b. Run Flask without Bash

If you don't have a bash application, you can run Flak without one. Below we give an example of how to run Flask without bash. Run the command below to run Flask without bash.

Run Flask without Bash
root@ns3:~ # cd /usr/local/etc/flask
root@ns3:/usr/local/etc/flask # flask --app flaskApp.py run --host=192.168.5.2 --debug
 * Serving Flask app 'flaskApp.py'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://192.168.5.2:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 996-541-309
With bash or without bash, the results displayed in Google Chrome will be the same. You just choose, run Flask with bash or without bash

c. Run Flask from the Github repository

For web application developers, Github is no longer foreign. Many of the Python packages you can use on FreeBSD come from Github. In this section, we will demonstrate how to run Flask whose Python package application comes from the Github repository. The first step, you clone the Python package that will be used. Below we have selected one Python package that we will deploy on the FreeBSD server.

Clone a Flask application
root@ns3:~ # cd /usr/local/etc/flask
root@ns3:/usr/local/etc/flask # git clone https://github.com/pallets/flask
root@ns3:/usr/local/etc/flask # cd FlaskApp
root@ns3:/usr/local/etc/flask/FlaskApp #
If the cloning process has finished, run the Initialize database command.

Initialized the database
root@ns3:/usr/local/etc/flask/FlaskApp # cd examples/tutorial
root@ns3:/usr/local/etc/flask/FlaskApp/examples/tutorial # flask --app flaskr init-db
Initialized the database.
After that, you run Flask (without bash).

Clone a Flask application
root@ns3:/usr/local/etc/flask/FlaskApp/examples/tutorial # flask --app flaskr run --host=192.168.5.2 --debug
 * Serving Flask app 'flaskr'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://192.168.5.2:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 996-541-309
You can see the results of the command above by opening Google Chrome. Type "http://192.168.5.2:5000/" in the address bar menu.

By reading the entire contents of this article, you have successfully created a web server with Python and Flask. Now you can explore the capabilities of Flask and develop web applications with the Python language. This material is only a small part of how to use Flask, we recommend that you study additional material and practice to improve your skills.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post