<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/" >

<channel>
	<title>Framework &#8211; Dakidarts® Hub</title>
	<atom:link href="https://hub.dakidarts.com/tag/framework/feed/" rel="self" type="application/rss+xml" />
	<link>https://hub.dakidarts.com</link>
	<description>Where creativity meets innovation.</description>
	<lastBuildDate>Fri, 16 Aug 2024 08:47:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://cdn.dakidarts.com/image/dakidarts-dws.svg</url>
	<title>Framework &#8211; Dakidarts® Hub</title>
	<link>https://hub.dakidarts.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Python for Web Development: Flask Framework Fundamentals</title>
		<link>https://hub.dakidarts.com/python-for-web-development-flask-framework-fundamentals/</link>
					<comments>https://hub.dakidarts.com/python-for-web-development-flask-framework-fundamentals/#respond</comments>
		
		<dc:creator><![CDATA[Dakidarts]]></dc:creator>
		<pubDate>Fri, 16 Aug 2024 08:41:21 +0000</pubDate>
				<category><![CDATA[Python 🪄]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Fundamentals]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Development]]></category>
		<guid isPermaLink="false">https://hub.dakidarts.com/?p=5466</guid>

					<description><![CDATA[Learn Python web development with Flask. This comprehensive guide covers Flask fundamentals, routing, templates, and database integration. Perfect for beginners building their first web application.]]></description>
										<content:encoded><![CDATA[
<div class="automaticx-video-container"><iframe src="https://www.youtube.com/embed/Z1RJmh_OqeA" width="100%" height="380" frameborder="0" allowfullscreen="allowfullscreen"></iframe></div>



<h2 class="wp-block-heading" id="unleashing-the-power-of-flask-an-introduction-to-python-web-development"><span id=""></span></h2>



<p class="wp-block-paragraph">Flask is a lightweight and flexible Python web framework that&#8217;s perfect for beginners and seasoned developers alike. In this tutorial, we&#8217;ll explore the fundamentals of Flask and build a simple web application from scratch. By the end, you&#8217;ll have a solid foundation in Flask web development.</p>



<h2 id="prerequisites" class="wp-block-heading">Prerequisites</h2>



<p class="wp-block-paragraph">Before we begin, make sure you have:</p>



<ul class="wp-block-list">
<li><a href="https://www.python.org/downloads/" target="_blank" rel="noreferrer noopener nofollow">Python 3.x installed</a></li>



<li>Basic knowledge of Python</li>



<li>A text editor or IDE of your choice</li>
</ul>



<h2 id="step-1-setting-up-your-environment" class="wp-block-heading">Step 1: Setting Up Your Environment</h2>



<p class="wp-block-paragraph">First, let&#8217;s create a virtual environment and install Flask:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Create a new directory for your project
mkdir flask_tutorial
cd flask_tutorial

# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`

# Install Flask
pip install flask</pre>



<h2 id="step-2-creating-your-first-flask-application" class="wp-block-heading">Step 2: Creating Your First Flask Application</h2>



<p class="wp-block-paragraph">Let&#8217;s create a simple &#8220;Hello, World!&#8221; application. Create a new file called <code data-enlighter-language="generic" class="EnlighterJSRAW">app.py</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(debug=True)</pre>



<p class="wp-block-paragraph">Run your application:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python app.py</pre>



<p class="wp-block-paragraph">Visit <code data-enlighter-language="bash" class="EnlighterJSRAW">http://127.0.0.1:5000/</code> in your browser, and you should see &#8220;Hello, World!&#8221;.</p>



<h2 id="step-3-understanding-routing" class="wp-block-heading">Step 3: Understanding Routing</h2>



<p class="wp-block-paragraph">Flask uses decorators to define routes. Let&#8217;s add more routes to our application:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Welcome to the Flask Tutorial!'

@app.route('/about')
def about():
    return 'This is the about page.'

@app.route('/user/&lt;username>')
def show_user_profile(username):
    return f'User: {username}'

if __name__ == '__main__':
    app.run(debug=True)</pre>



<p class="wp-block-paragraph">Now you have three routes:</p>



<ul class="wp-block-list">
<li><code data-enlighter-language="generic" class="EnlighterJSRAW">/</code>: The home page</li>



<li><code data-enlighter-language="bash" class="EnlighterJSRAW">/about</code>: An about page</li>



<li><code data-enlighter-language="bash" class="EnlighterJSRAW">/user/&lt;username></code>: A dynamic route that accepts a username</li>
</ul>



<h2 id="step-4-working-with-templates" class="wp-block-heading">Step 4: Working with Templates</h2>



<p class="wp-block-paragraph">Instead of returning plain text, let&#8217;s use HTML templates. First, create a <code data-enlighter-language="generic" class="EnlighterJSRAW">templates</code> folder in your project directory.</p>



<p class="wp-block-paragraph">Create <code data-enlighter-language="bash" class="EnlighterJSRAW">templates/base.html</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!DOCTYPE html>
&lt;html lang="en">
&lt;head>
    &lt;meta charset="UTF-8">
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
    &lt;title>{% block title %}Flask Tutorial{% endblock %}&lt;/title>
&lt;/head>
&lt;body>
    &lt;nav>
        &lt;a href="{{ url_for('home') }}">Home&lt;/a>
        &lt;a href="{{ url_for('about') }}">About&lt;/a>
    &lt;/nav>
    &lt;main>
        {% block content %}{% endblock %}
    &lt;/main>
&lt;/body>
&lt;/html></pre>



<p class="wp-block-paragraph">Create <code data-enlighter-language="bash" class="EnlighterJSRAW">templates/home.html</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{% extends "base.html" %}

{% block content %}
&lt;h1>Welcome to the Flask Tutorial!&lt;/h1>
&lt;p>This is the home page.&lt;/p>
{% endblock %}</pre>



<p class="wp-block-paragraph">Create <code data-enlighter-language="bash" class="EnlighterJSRAW">templates/about.html</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{% extends "base.html" %}

{% block title %}About - {{ super() }}{% endblock %}

{% block content %}
&lt;h1>About&lt;/h1>
&lt;p>This is the about page of our Flask tutorial.&lt;/p>
{% endblock %}</pre>



<p class="wp-block-paragraph">Now, update <code data-enlighter-language="generic" class="EnlighterJSRAW">app.py</code> to use these templates:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/user/&lt;username>')
def show_user_profile(username):
    return render_template('user.html', username=username)

if __name__ == '__main__':
    app.run(debug=True)</pre>



<p class="wp-block-paragraph">Create <code data-enlighter-language="bash" class="EnlighterJSRAW">templates/user.html</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{% extends "base.html" %}

{% block title %}User: {{ username }} - {{ super() }}{% endblock %}

{% block content %}
&lt;h1>User Profile&lt;/h1>
&lt;p>Username: {{ username }}&lt;/p>
{% endblock %}</pre>



<h2 id="step-5-handling-forms" class="wp-block-heading">Step 5: Handling Forms</h2>



<p class="wp-block-paragraph">Let&#8217;s add a simple form to our application. Create <code data-enlighter-language="bash" class="EnlighterJSRAW">templates/form.html</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{% extends "base.html" %}

{% block title %}Form - {{ super() }}{% endblock %}

{% block content %}
&lt;h1>Sample Form&lt;/h1>
&lt;form method="POST">
    &lt;label for="name">Name:&lt;/label>
    &lt;input type="text" id="name" name="name" required>
    &lt;button type="submit">Submit&lt;/button>
&lt;/form>
{% if name %}
&lt;p>Hello, {{ name }}!&lt;/p>
{% endif %}
{% endblock %}</pre>



<p class="wp-block-paragraph">Update <code data-enlighter-language="generic" class="EnlighterJSRAW">app.py</code> to handle the form:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from flask import Flask, render_template, request

app = Flask(__name__)

# ... (previous routes) ...

@app.route('/form', methods=['GET', 'POST'])
def form():
    name = None
    if request.method == 'POST':
        name = request.form['name']
    return render_template('form.html', name=name)

if __name__ == '__main__':
    app.run(debug=True)</pre>



<h2 id="step-6-database-integration" class="wp-block-heading">Step 6: Database Integration</h2>



<p class="wp-block-paragraph">For this example, we&#8217;ll use SQLite with SQLAlchemy. First, install the required packages:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install flask-sqlalchemy</pre>



<p class="wp-block-paragraph">Update <code data-enlighter-language="generic" class="EnlighterJSRAW">app.py</code> to include database functionality:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __repr__(self):
        return f'&lt;User {self.username}>'

# Create the database tables
with app.app_context():
    db.create_all()

@app.route('/')
def home():
    users = User.query.all()
    return render_template('home.html', users=users)

@app.route('/add_user', methods=['GET', 'POST'])
def add_user():
    if request.method == 'POST':
        username = request.form['username']
        email = request.form['email']
        new_user = User(username=username, email=email)
        db.session.add(new_user)
        db.session.commit()
        return redirect(url_for('home'))
    return render_template('add_user.html')

# ... (other routes) ...

if __name__ == '__main__':
    app.run(debug=True)</pre>



<p class="wp-block-paragraph">Create <code data-enlighter-language="bash" class="EnlighterJSRAW">templates/add_user.html</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{% extends "base.html" %}

{% block title %}Add User - {{ super() }}{% endblock %}

{% block content %}
&lt;h1>Add User&lt;/h1>
&lt;form method="POST">
    &lt;label for="username">Username:&lt;/label>
    &lt;input type="text" id="username" name="username" required>
    &lt;label for="email">Email:&lt;/label>
    &lt;input type="email" id="email" name="email" required>
    &lt;button type="submit">Add User&lt;/button>
&lt;/form>
{% endblock %}</pre>



<p class="wp-block-paragraph">Update <code data-enlighter-language="bash" class="EnlighterJSRAW">templates/home.html</code> to display users:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{% extends "base.html" %}

{% block content %}
&lt;h1>Welcome to the Flask Tutorial!&lt;/h1>
&lt;p>This is the home page.&lt;/p>
&lt;h2>Users:&lt;/h2>
&lt;ul>
    {% for user in users %}
    &lt;li>{{ user.username }} ({{ user.email }})&lt;/li>
    {% endfor %}
&lt;/ul>
&lt;a href="{{ url_for('add_user') }}">Add User&lt;/a>
{% endblock %}</pre>



<h2 id="conclusion" class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">Congratulations! You&#8217;ve now created a basic Flask web application with routing, templates, form handling, and database integration. This tutorial covered the fundamentals of Flask, but there&#8217;s much more to explore:</p>



<ul class="wp-block-list">
<li>User authentication and authorization</li>



<li>RESTful API development</li>



<li>Flask extensions for additional functionality</li>



<li>Deployment to production servers</li>
</ul>



<p class="wp-block-paragraph">As you continue your journey with Flask, remember to consult the official Flask documentation for more detailed information and advanced topics. Happy coding, and may your web development adventures with Flask be ever exciting!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://hub.dakidarts.com/python-for-web-development-flask-framework-fundamentals/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<media:content url="https://cdn.dakidarts.com/image/5466-python-for-web-development-flask-framework-fundamentals.jpg" medium="image"></media:content>
            <media:content url="https://www.youtube.com/embed/Z1RJmh_OqeA" medium="video">
			<media:player url="https://www.youtube.com/embed/Z1RJmh_OqeA" />
			<media:title type="plain">Read Insightful Framework Articles - Dakidarts® Hub</media:title>
			<media:description type="html"><![CDATA[Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.]]></media:description>
			<media:thumbnail url="https://cdn.dakidarts.com/image/5466-python-for-web-development-flask-framework-fundamentals.jpg" />
			<media:rating scheme="urn:simple">nonadult</media:rating>
		</media:content>
	</item>
	</channel>
</rss>
