Flask is a minimalistic web framework that enables developers to build web applications quickly using Python. It is easy to use, flexible, and has a large ecosystem of extensions. Below is a fully working example of a Flask application featuring templates, static files, form handling, and basic SQLite database integration.
flask_app/
βββ app.py
βββ templates/
β βββ base.html
β βββ index.html
β βββ submit.html
βββ static/
β βββ style.css
βββ database.db
pip install flask
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
DATABASE = 'database.db'
def get_db():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
@app.route('/')
def index():
conn = get_db()
posts = conn.execute('SELECT * FROM posts').fetchall()
conn.close()
return render_template('index.html', posts=posts)
@app.route('/submit', methods=['GET', 'POST'])
def submit():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
conn = get_db()
conn.execute('INSERT INTO posts (title, content) VALUES (?, ?)', (title, content))
conn.commit()
conn.close()
return redirect(url_for('index'))
return render_template('submit.html')
if __name__ == '__main__':
app.run(debug=True)
import sqlite3
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT, content TEXT)')
conn.commit()
conn.close()
<!DOCTYPE html>
<html>
<head>
<title>Flask Blog</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>Flask Blog App</h1>
<div>
{% block content %}{% endblock %}
</div>
</body>
</html>
{% extends 'base.html' %}
{% block content %}
<a href="{{ url_for('submit') }}">Create New Post</a>
<ul>
{% for post in posts %}
<li><b>{{ post['title'] }}:</b> {{ post['content'] }}</li>
{% endfor %}
</ul>
{% endblock %}
{% extends 'base.html' %}
{% block content %}
<form method="post">
<input type="text" name="title" placeholder="Title"><br>
<textarea name="content" placeholder="Content"></textarea><br>
<input type="submit" value="Submit">
</form>
{% endblock %}
/* static/style.css */
body {
font-family: Arial, sans-serif;
}
input, textarea {
margin: 5px 0;
padding: 5px;
width: 300px;
}
python app.py
When visiting http://localhost:5000, you can view posts and add new entries through the form interface.
You can easily expand this by adding features like user authentication, pagination, AJAX integration, or deploying with Gunicorn + Nginx or Heroku.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved