{"id":3889,"date":"2023-11-04T23:13:54","date_gmt":"2023-11-04T23:13:54","guid":{"rendered":"http:\/\/localhost:10003\/how-to-create-a-web-app-with-flask-and-python\/"},"modified":"2023-11-05T05:48:29","modified_gmt":"2023-11-05T05:48:29","slug":"how-to-create-a-web-app-with-flask-and-python","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-create-a-web-app-with-flask-and-python\/","title":{"rendered":"How to Create a Web App with Flask and Python"},"content":{"rendered":"
In this tutorial, we will learn how to create a web application using Flask, a lightweight web framework for Python. Flask allows us to quickly build web applications that are simple, scalable, and easy to understand.<\/p>\n
To follow along with this tutorial, you should have a basic understanding of Python and web development concepts. Let’s get started!<\/p>\n
First, we need to set up a virtual environment to isolate our project’s dependencies. This will ensure that our application runs consistently across different environments.<\/p>\n
mkdir myapp<\/code>.<\/li>\n- Navigate to the project directory:
cd myapp<\/code>.<\/li>\n- Create a new virtual environment:
python3 -m venv venv<\/code>.<\/li>\n- Activate the virtual environment:\n
\n- On macOS and Linux:
source venv\/bin\/activate<\/code>.<\/li>\n- On Windows:
venvScriptsactivate<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\nStep 2: Install Flask<\/h2>\n
Now that we have our virtual environment set up, let’s install Flask.<\/p>\n
\n- Make sure you are still in the project directory.<\/li>\n
- Install Flask using pip:
pip install flask<\/code>.<\/li>\n<\/ol>\nStep 3: Create the Flask Application<\/h2>\n
With Flask installed, let’s create our Flask application.<\/p>\n
\n- Create a new file called
app.py<\/code> in your project directory.<\/li>\n- Open
app.py<\/code> in your favorite text editor.<\/li>\n<\/ol>\nIn app.py<\/code>, we will import Flask and create our Flask application.<\/p>\nfrom flask import Flask\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef hello():\n return 'Hello, Flask!'\n<\/code><\/pre>\nIn this code, we import the Flask<\/code> class and create an instance of it called app<\/code>. We then use the @app.route()<\/code> decorator to define a route for the root URL (“\/”). When a user visits the root URL, the hello()<\/code> function will be called and it will return the string ‘Hello, Flask!’.<\/p>\nStep 4: Run the Flask Application<\/h2>\n
Now that we have our Flask application set up, let’s run it and see the results!<\/p>\n
\n- Make sure you are still in the project directory.<\/li>\n
- Start the Flask development server:
flask run<\/code>.<\/li>\n<\/ol>\nYou should see output similar to the following:<\/p>\n
* Running on http:\/\/127.0.0.1:5000\/ (Press CTRL+C to quit)\n<\/code><\/pre>\nOpen your web browser and visit `http:\/\/127.0.0.1:5000\/`. You should see the message ‘Hello, Flask!’ displayed on the page.<\/p>\n
Congratulations! You have created a basic Flask web application.<\/p>\n
Step 5: Routing and Templates<\/h2>\n
In the previous step, we defined a single route for the root URL. Let’s add more routes and use templates to render dynamic content.<\/p>\n
\n- Update
app.py<\/code> with the following code:<\/li>\n<\/ol>\nfrom flask import Flask, render_template\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef home():\n return render_template('index.html')\n\n@app.route('\/about')\ndef about():\n return render_template('about.html')\n<\/code><\/pre>\n\n- Create a new directory called
templates<\/code> in your project directory.<\/li>\n- Inside the
templates<\/code> directory, create two files: index.html<\/code> and about.html<\/code>.<\/li>\n<\/ol>\nIn index.html<\/code>, add the following code:<\/p>\n<!DOCTYPE html>\n<html>\n<head>\n <title>Home<\/title>\n<\/head>\n<body>\n <h1>Welcome to the Home Page!<\/h1>\n <p>Feel free to explore the website.<\/p>\n<\/body>\n<\/html>\n<\/code><\/pre>\nIn about.html<\/code>, add the following code:<\/p>\n<!DOCTYPE html>\n<html>\n<head>\n <title>About<\/title>\n<\/head>\n<body>\n <h1>About Us<\/h1>\n <p>We are a team of developers passionate about Flask!<\/p>\n<\/body>\n<\/html>\n<\/code><\/pre>\nNow, if you start the Flask development server again and visit http:\/\/127.0.0.1:5000\/`, you will see the content from<\/code>index.html. If you visit<\/code>http:\/\/127.0.0.1:5000\/about`, you will see the content from about.html<\/code>.<\/p>\nFlask uses the Jinja templating engine to render HTML templates. By using templates, we can separate our application logic from the presentation layer and create reusable components.<\/p>\n
Step 6: Handling Form Data<\/h2>\n
To make our web application more interactive, let’s handle form data.<\/p>\n
\n- Update
app.py<\/code> with the following code:<\/li>\n<\/ol>\nfrom flask import Flask, render_template, request\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef home():\n return render_template('index.html')\n\n@app.route('\/contact', methods=['GET', 'POST'])\ndef contact():\n if request.method == 'POST':\n name = request.form.get('name')\n email = request.form.get('email')\n message = request.form.get('message')\n\n # Process the form data (e.g. send an email, store in database)\n\n return 'Thank you for your message, {}!'.format(name)\n\n return render_template('contact.html')\n<\/code><\/pre>\n\n- Inside the
templates<\/code> directory, create a new file called contact.html<\/code>.<\/li>\n<\/ol>\nIn contact.html<\/code>, add the following code:<\/p>\n<!DOCTYPE html>\n<html>\n<head>\n <title>Contact<\/title>\n<\/head>\n<body>\n <h1>Contact Us<\/h1>\n\n <form method=\"POST\" action=\"\/contact\">\n <label for=\"name\">Name:<\/label>\n <input type=\"text\" id=\"name\" name=\"name\" required>\n\n <label for=\"email\">Email:<\/label>\n <input type=\"email\" id=\"email\" name=\"email\" required>\n\n <label for=\"message\">Message:<\/label>\n <textarea id=\"message\" name=\"message\" required><\/textarea>\n\n <button type=\"submit\">Submit<\/button>\n <\/form>\n<\/body>\n<\/html>\n<\/code><\/pre>\nNow, when you visit http:\/\/127.0.0.1:5000\/contact`, you will see a form where you can enter your name, email, and message. When you submit the form, the form data will be sent to the<\/code>\/contact` route where it will be processed. In this example, we simply return a thank you message to the user.<\/p>\nStep 7: Adding Static Files<\/h2>\n
Flask also makes it easy to serve static files such as CSS stylesheets, JavaScript files, and images.<\/p>\n
\n- Create a new directory called
static<\/code> in your project directory.<\/li>\n- Inside the
static<\/code> directory, create a file called styles.css<\/code>.<\/li>\n<\/ol>\nIn styles.css<\/code>, add some CSS code:<\/p>\nbody {\n font-family: Arial, sans-serif;\n}\n\nh1 {\n color: #ff0000;\n}\n\np {\n color: #0000ff;\n}\n<\/code><\/pre>\nTo use this stylesheet in your templates, update index.html<\/code> and about.html<\/code> as follows:<\/p>\n<!DOCTYPE html>\n<html>\n<head>\n <title>Home<\/title>\n <link rel=\"stylesheet\" type=\"text\/css\" href=\"{{ url_for('static', filename='styles.css') }}\">\n<\/head>\n<body>\n <h1>Welcome to the Home Page!<\/h1>\n <p>Feel free to explore the website.<\/p>\n<\/body>\n<\/html>\n<\/code><\/pre>\n<!DOCTYPE html>\n<html>\n<head>\n <title>About<\/title>\n <link rel=\"stylesheet\" type=\"text\/css\" href=\"{{ url_for('static', filename='styles.css') }}\">\n<\/head>\n<body>\n <h1>About Us<\/h1>\n <p>We are a team of developers passionate about Flask!<\/p>\n<\/body>\n<\/html>\n<\/code><\/pre>\nRestart the Flask development server and refresh the pages. You should see the text styled according to the CSS rules.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we covered the basics of creating a web application with Flask and Python. We learned how to set up a virtual environment, install Flask, create routes, use templates, handle form data, and serve static files.<\/p>\n
Flask provides a powerful yet simple way to create web applications in Python. By following this tutorial, you should now have a good foundation to build upon and create more complex applications.<\/p>\n
Keep practicing and experimenting with Flask to explore its full potential. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"
In this tutorial, we will learn how to create a web application using Flask, a lightweight web framework for Python. Flask allows us to quickly build web applications that are simple, scalable, and easy to understand. To follow along with this tutorial, you should have a basic understanding of Python Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[136,139,135,137,138,134],"yoast_head":"\nHow to Create a Web App with Flask and Python - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n