<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Python, Flask, Django Tutorials and Tips - Medium]]></title>
        <description><![CDATA[Tutorials for the Python programming language and its popular frameworks like Flask and Django. - Medium]]></description>
        <link>https://medium.com/python-flask-django-tutorials-and-tips?source=rss----914a5035b415---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Python, Flask, Django Tutorials and Tips - Medium</title>
            <link>https://medium.com/python-flask-django-tutorials-and-tips?source=rss----914a5035b415---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:05:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/python-flask-django-tutorials-and-tips" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Net Income Calculator Application Using Python]]></title>
            <link>https://medium.com/python-flask-django-tutorials-and-tips/building-a-net-income-calculator-application-using-python-becd81976f82?source=rss----914a5035b415---4</link>
            <guid isPermaLink="false">https://medium.com/p/becd81976f82</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[John]]></dc:creator>
            <pubDate>Sat, 09 Apr 2016 15:31:46 GMT</pubDate>
            <atom:updated>2019-11-30T09:42:57.155Z</atom:updated>
            <content:encoded><![CDATA[<h3>Build a Net Income Calculator App with Python — pp</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/0*GxRC6SyyGc8S9ltb.jpg" /></figure><p>This tutorial is for beginners who want to learn how to build applications in Python programming language.</p><p><strong>Important Note</strong></p><p>If you’re using <strong>Python 3</strong>, you should use:</p><ul><li><strong>input()</strong> instead of <strong>raw_input()</strong></li><li><strong>print()</strong> instead of <strong>print</strong>. Example: instead of <strong>print “hello”,</strong> you should use <strong>print(“hello”)</strong></li></ul><p><strong>What will we build?</strong></p><p>We will build a net income calculator for freelancers in an imaginary company that serves as a middleman between a freelancer and an employer. To be able to focus on Python, we won’t build a web based application so you won’t have to use HTML. What we will build is a very simple command line application.</p><p><strong>Requirements</strong></p><ul><li>The user should be able to input his/her hourly rate.</li><li>The user should also be able to input the number of hours he/she worked.</li><li>We will assume that our imaginary company deducts $1 + 10% of the gross income of the user .</li><li>The gross income, net income and deductions should be shown to the user.</li></ul><p><strong>Building the command line application</strong></p><p>Create a file and name it <strong>income_calculator.py</strong></p><p>Before starting to write code, it’s a good practice to plan how you will build the application first. Let’s do that by adding comments in <strong>income_calculator.py</strong></p><pre># ask the hourly rate of the user</pre><pre># convert hourly rate to float</pre><pre># ask the number of hours that the user has worked</pre><pre># convert hours worked to float</pre><pre># multiply the hourly rate and the number of hours worked</pre><pre># get the 10% of the gross income</pre><pre># add 1 to the deduction</pre><pre># subtract the deductions to the gross income</pre><pre># show the gross income, net income and deductions to the user</pre><p>Now that we have a clear idea on what are the steps that we should code to achieve the expected result, let’s start writing code.</p><pre># ask the hourly rate of the user hourly_rate = raw_input(&quot;What is your hourly rate?&quot;)</pre><p><strong>raw_input()</strong> is a built-in function in Python that we can use to gather inputs from a user. What this line of code basically does is ask the hourly rate of the user and stores the input of the user in the <strong>hourly_rate</strong> variable.</p><pre># convert hourly rate to float hourly_rate = float(hourly_rate)</pre><p>The result of <strong>raw_input()</strong> is a string so we are converting it to a float for us to be able to use it for math operations later.</p><pre># ask the number of hours that the user has worked hours_worked = raw_input(&quot;How many hours did you worked?&quot;)</pre><pre># convert hours worked to float hours_worked = float(hours_worked)</pre><p>What the code above does is similar to the code that I’ve discussed earlier. It just asks the number of hours that the user worked, store it in the <strong>hours_worked</strong> variable then converts it to a float.</p><pre># multiply the hourly rate and the number of hours worked gross_income = hourly_rate * hours_worked</pre><p>To get the gross income of the user, we are multiplying the user’s hourly rate and the number of hours worked. We are storing the result in the <strong>gross_income</strong> variable.</p><pre># get the 10% of the gross income deduction = gross_income * .10</pre><p>We are multiplying gross income and .10 to get the 10% of gross income. We are storing the result in the <strong>deduction</strong> variable.</p><pre># add 1 to the deduction total_deductions = deduction + 1</pre><p>Since the company also deducts $1 to the gross income of the user, we are adding 1 to the value of deduction variable and we are storing the result to <strong>total_deductions</strong> variable.</p><pre># subtract the deductions to the gross income net_income = gross_income - total_deductions</pre><p>To compute the net income of the user, we are subtracting the total deductions to the gross income.</p><pre># show the gross income, net income and deductions to the user print &quot;Gross income: &quot; + str(gross_income) print &quot;Net income: &quot; + str(net_income) print &quot;Total deductions: &quot; + str(total_deductions)</pre><p>This block of code exactly does what the comment says. It will show the gross income, net income and deductions to the user. In Python, since we can’t concatenate a string (Example: “Gross income”) and a float (gross_income), we have to convert gross_income to a string. We can convert a float to a string by using Python’s built-in function <strong>str()</strong></p><p>The contents of <strong>income_calculator.py </strong>should now look like this:</p><pre># ask the hourly rate of the user hourly_rate = raw_input(&quot;What is your hourly rate?&quot;)</pre><pre># convert hourly rate to float hourly_rate = float(hourly_rate)</pre><pre># ask the number of hours that the user has worked hours_worked = raw_input(&quot;How many hours did you worked?&quot;)</pre><pre># convert hours worked to float hours_worked = float(hours_worked)</pre><pre># multiply the hourly rate and the number of hours worked gross_income = hourly_rate * hours_worked</pre><pre># get the 10% of the gross income deduction = gross_income * .10</pre><pre># add 1 to the deduction total_deductions = deduction + 1</pre><pre># subtract the deductions to the gross income net_income = gross_income - total_deductions</pre><pre># show the gross income, net income and deductions to the user print &quot;Gross income: &quot; + str(gross_income) print &quot;Net income: &quot; + str(net_income) print &quot;Total deductions: &quot; + str(total_deductions)</pre><p>Now let’s run our command line application by running this command in your terminal:</p><pre>python income_calculator.py</pre><p>It should ask your hourly rate and number of hours worked then show your gross income, net income and total deductions.</p><p>Feel free to leave a feedback about the tutorial by leaving a comment. I would really appreciate it if you will recommend and also tweet this tutorial 🙂</p><p>Want to get updated when I publish another tutorial? Follow me here on Twitter: <a href="https://twitter.com/John200Ok">https://twitter.com/John200Ok</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=becd81976f82" width="1" height="1" alt=""><hr><p><a href="https://medium.com/python-flask-django-tutorials-and-tips/building-a-net-income-calculator-application-using-python-becd81976f82">Building a Net Income Calculator Application Using Python</a> was originally published in <a href="https://medium.com/python-flask-django-tutorials-and-tips">Python, Flask, Django Tutorials and Tips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a CRUD application using Flask (Python Framework) Part 2]]></title>
            <link>https://medium.com/python-flask-django-tutorials-and-tips/how-to-build-a-crud-application-using-flask-python-framework-part-2-6859b4730350?source=rss----914a5035b415---4</link>
            <guid isPermaLink="false">https://medium.com/p/6859b4730350</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[John]]></dc:creator>
            <pubDate>Fri, 08 Apr 2016 00:25:58 GMT</pubDate>
            <atom:updated>2019-11-30T09:48:56.517Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mm84lpl94qz-SE8tKD6U1A.png" /></figure><p>So let’s remove our custom __init__ method. Your note model should now only look like this:</p><pre>class Note(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80)) body = db.Column(db.Text)</pre><p>We’ll now add a create a note page.</p><p>In <strong>main.py</strong>:</p><p>Above:</p><pre>if __name__ == &quot;__main__&quot;: app.run(debug=True)</pre><p>Add this code:</p><pre><a href="http://web.archive.org/web/20170415231342/http://twitter.com/app">@app</a>.route(&quot;/notes/create&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])</pre><p>This route will handle both <strong>GET</strong> and <strong>POST</strong> HTTP methods.</p><p>If you’re not familiar with HTTP methods, you can refer here: <a href="http://web.archive.org/web/20170415231342/http://www.tutorialspoint.com/http/http_methods.htm">http://www.tutorialspoint.com/http/http_methods.htm</a></p><p>Next, add this code below the route that we added above.</p><p>In <strong>main.py:</strong></p><pre>def create_note(): if request.method == &quot;GET&quot;: return render_template(&quot;create_note.html&quot;) else: title = request.form[&quot;title&quot;] body = request.form[&quot;body&quot;]</pre><pre>note = Note(title=title, body=body)</pre><pre>db.session.add(note) db.session.commit()</pre><pre>return redirect(&quot;/notes/create&quot;)</pre><p>If the request method is <em>GET</em>, our function above will render the <strong>create_note.html</strong> template. If the request method is not equal to <strong>GET (</strong> In our case, if the request method is not equal to GET, we are assuming that the request method is equal to POST. We are sure that the request method is equal to POST if its not equal to GET because our route only allows GET and POST)</p><p>Check the route that we added above and you’ll see this:</p><pre>methods=[&quot;GET&quot;, &quot;POST&quot;]</pre><p>Now back to what I was saying, if the request method is not equal to <strong>GET, </strong>we are retrieving the submitted <em>title</em> and <em>body </em>by the user using <strong>request.form[]</strong> and passing its values to the <strong>title</strong> variable and <strong>body</strong> variable. Next, we are instantiating a <strong>Note</strong> model and passing the <strong>title</strong> and <strong>body</strong> variables into it. The <strong>Note</strong> instance is passed to the <strong>note</strong> variable.</p><p>Next, we are passing the <strong>note</strong> variable to <strong>db.session.add()</strong>.</p><p>And then we are calling <strong>db.session.commit()</strong> to save the note to the database. Then we are redirecting the user to the same page.</p><p>If you want to understand how SQLAlchemy’s session works, read this article: <a href="http://web.archive.org/web/20170415231342/http://pythoncentral.io/understanding-python-sqlalchemy-session/">http://pythoncentral.io/understanding-python-sqlalchemy-session/</a></p><p>In <strong>templates</strong> folder, create a file and name it <strong>create_note.html</strong> then put this code inside it:</p><pre>&lt;form action=&quot;/notes/create&quot; method=&quot;POST&quot;&gt; &lt;label&gt;Title&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;title&quot;&gt;</pre><pre>&lt;label&gt;Body&lt;/label&gt; &lt;input type=&quot;text&quot; name=&quot;body&quot;&gt;</pre><pre>&lt;input type=&quot;submit&quot; value=&quot;Create&quot;&gt; &lt;/form&gt;</pre><p>What we did here is just add an HTML form that has 2 labels, 2 text boxes and a submit button. The 2 text boxes will allow the user to input a title and a description for the post. We also set the <strong>method</strong> attribute of the form to <strong>POST</strong>. If we don’t specify a method, it will use <strong>GET</strong> since it’s the default.</p><p>If you’re not familiar with HTML forms, you can refer here: <a href="http://web.archive.org/web/20170415231342/http://www.w3schools.com/html/html_forms.asp">http://www.w3schools.com/html/html_forms.asp</a></p><p>Now try visiting <a href="http://web.archive.org/web/20170415231342/http://localhost:5000/notes/create">http://localhost:5000/notes/create</a> in your browser. You should see this:</p><p>If you see an error like “Page not found” or “This site can’t be reached”. It’s probably because your server is not running. So make sure to check if the server is running. If it’s not running, run the server using this command while you’re in inside the <strong>note_app</strong> folder:</p><pre>python main.py</pre><p>While you’re in the create note page, enter a title and a body and then click the create a button. You should be redirected to the same page but now the inputs should now be gone.</p><p>Now let’s check if the note has been saved in the database. In this tutorial, we will use a tool named SQLite manager: <a href="http://web.archive.org/web/20170415231342/https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/">https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/</a> . It is a Firefox browser add-on that we can use to manage SQLite databases. Install it and then open your Firefox browser, click tools and then click SQLite manager:</p><p>Find the folder icon in the SQLite manager and click it. Open the SQLite database of your application by opening <strong>app.sqlite</strong> which is found under the <strong>note_app</strong> folder.</p><p>Now click <strong>Tables</strong>, click <strong>note</strong> and click <strong>Browse &amp; Search</strong>. You should now be able to see the note that we created earlier.</p><p><strong>What’s next?</strong></p><p>In the next part of the tutorial, we will create a note list page which basically lists all the notes that are added in the database. And maybe also add an edit note feature too! 🙂</p><p>Feel free to leave a feedback about the tutorial by leaving a comment. I would really appreciate it if you will recommend and also tweet this tutorial 🙂</p><p>Want to get updated when I publish another tutorial? Follow me here on Twitter: <a href="https://twitter.com/John200Ok">https://twitter.com/John200Ok</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6859b4730350" width="1" height="1" alt=""><hr><p><a href="https://medium.com/python-flask-django-tutorials-and-tips/how-to-build-a-crud-application-using-flask-python-framework-part-2-6859b4730350">How to build a CRUD application using Flask (Python Framework) Part 2</a> was originally published in <a href="https://medium.com/python-flask-django-tutorials-and-tips">Python, Flask, Django Tutorials and Tips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a CRUD application using Flask (Python Framework)]]></title>
            <link>https://medium.com/python-flask-django-tutorials-and-tips/how-to-build-a-crud-application-using-flask-python-framework-3a398b04c3c6?source=rss----914a5035b415---4</link>
            <guid isPermaLink="false">https://medium.com/p/3a398b04c3c6</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[John]]></dc:creator>
            <pubDate>Fri, 08 Apr 2016 00:25:31 GMT</pubDate>
            <atom:updated>2019-11-30T09:53:15.213Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ou6FFJJD3zhcIUU8wBZqIw.png" /></figure><blockquote>Share this tutorial w/ your followers on Twitter. Click this link to tweet this tutorial: <a href="https://goo.gl/MtIA1L">https://goo.gl/MtIA1L</a></blockquote><p>The application that we will build is a very simple database driven note taking application. This tutorial is for beginners who want to learn how to use Flask for developing websites or web applications.</p><p>This tutorial assumes that you already installed Python and SQlite. If Python and SQlite is not yet installed in your computer, install them first. If the Python version that you installed is 2.5 and up, then you don’t need to install SQlite since Python 2.5 and up already comes with SQlite.</p><p>Python: <a href="https://www.python.org/">https://www.python.org/</a></p><p>SQlite: <a href="https://www.sqlite.org/download.html">https://www.sqlite.org/download.html</a></p><p><strong>Things that we will cover in this tutorial:</strong></p><ul><li>Using templates</li><li>Saving a record in the database</li><li>Fetching a record in the database</li><li>Updating a record in the database</li><li>Deleting a record in the database</li></ul><p><strong>App requirements:</strong></p><ul><li>The user should be able to create a note (a note can have a title and a body)</li><li>The user should be able to view a list of notes</li><li>The user should be able to edit a note</li><li>The user should be able to delete a note</li></ul><p>Okay, let’s start.</p><p>We first need to install <strong>Flask</strong> and <strong>Flask-SQLAlchemy</strong>.</p><p>Open a terminal window (command line in Windows) and run these commands one after another:</p><pre>pip install Flask</pre><pre>pip install Flask-SQLAlchemy</pre><p>Now create a folder where you will put the source code of the application. Let’s use <strong>note_app</strong> as the name of the folder.</p><p>Inside the folder that you created, create a file and name it <strong>main.py</strong></p><p>We will be using templates in this tutorial so let’s also create a folder where we will put the templates. Use <strong>templates</strong> as the name of the folder. The templates folder should be inside the <strong>note_app</strong> folder. When using templates, Flask will automatically look for the template in the templates folder, that’s why we are using templates as the name of the folder.</p><p>Reference: <a href="http://flask.pocoo.org/docs/0.10/quickstart/#rendering-templates">http://flask.pocoo.org/docs/0.10/quickstart/#rendering-templates</a></p><p>Now let’s start writing code.</p><p>In main.py:</p><pre>from flask import Flask, render_template, redirect, request<br>from flask_sqlalchemy import SQLAlchemy<br>import os</pre><p>Next, we create our actual application and create the SQLAlchemy object by passing it the application.</p><pre>app = Flask(__name__)</pre><pre>basedir = os.path.abspath(os.path.dirname(__file__))<br>app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘sqlite:///’ + os.path.join(basedir, ‘app.sqlite’)<br>db = SQLAlchemy(app)</pre><blockquote>Gain the skills you need to be a pro at <strong>Python</strong> development. Visit: <a href="http://treehouse.7eer.net/c/245500/245646/3944">http://treehouse.7eer.net/c/245500/245646/3944</a></blockquote><p>Next, we will create a model:</p><pre>class Note(db.Model):<br>    id = db.Column(db.Integer, primary_key=True)<br>    title = db.Column(db.String(80))<br>    body = db.Column(db.Text)</pre><pre>    def __init__(self, title, body):<br>        self.title = title<br>        self.body = body</pre><p>The Note model above will be used for creating, fetching, updating and deleting a note in the database. Our Note model has an id attribute which is an integer and will serve as the primary key, a title which is a string which can contain a maximum of 80 characters (you can change the maximum number of characters if you want, we’re just setting it to 80 in this tutorial), and a body which is a text.</p><p>Integer, String and Text are data types, you can view a list of available data types that you can use in the documentation of SQLAlchemy: <a href="http://docs.sqlalchemy.org/en/latest/core/type_basics.html#generic-types">http://docs.sqlalchemy.org/en/latest/core/type_basics.html#generic-types</a></p><p>Go to your terminal window and run the following commands one after another:</p><pre>python<br>from main import db<br>db.create_all()</pre><p>The commands above will initialize the database. It will create the database and a table named <em>note</em>.</p><p>Now we will create a function and we will use the <strong>route()</strong> decorator to bind the function to a URL.</p><pre><a href="http://twitter.com/app">@app</a>.route(“/”)<br>def home():<br>    return render_template(“home.html”)</pre><p>Basically, what will happen is when the user visits the home page (example: <a href="http://mywebsite.com)/">http://mywebsite.com)</a> of our website, flask will use the <strong>home()</strong> function that we defined above. Inside the home() function, we are rendering a template called <strong>home.html</strong></p><p>Reference: <a href="http://flask.pocoo.org/docs/0.10/quickstart/#routing">http://flask.pocoo.org/docs/0.10/quickstart/#routing</a></p><p>Now open home.html and paste the contents below:</p><pre>&lt;h3&gt;Note Taking App&lt;/h3&gt;<br>&lt;a href=”/notes”&gt;View all notes&lt;/a&gt;<br>&lt;br&gt;<br>&lt;a href=”/notes/create”&gt;Create a note&lt;/a&gt;</pre><p>Go back to main.py and paste the code below in the bottom of the file:</p><pre>if __name__ == “__main__”:<br>    app.run(debug=True)</pre><p>The contents of your main.py file should now look similar to this:</p><pre>from flask import Flask, render_template, redirect, request<br>from flask_sqlalchemy import SQLAlchemy<br>import os</pre><pre>app = Flask(__name__)</pre><pre>basedir = os.path.abspath(os.path.dirname(__file__))<br>app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘sqlite:///’ + os.path.join(basedir, ‘app.sqlite’)<br>db = SQLAlchemy(app)</pre><pre>class Note(db.Model):<br>    id = db.Column(db.Integer, primary_key=True)<br>    title = db.Column(db.String(80))<br>    body = db.Column(db.Text)</pre><pre>    def __init__(self, title, body):<br>        self.title = title<br>        self.body = body</pre><pre><a href="http://twitter.com/app">@app</a>.route(“/”)<br>def home():<br>    return render_template(“home.html”)</pre><pre>if __name__ == “__main__”:<br>    app.run(debug=True)</pre><p>Now let’s run the application by running this command in your terminal window <em>(make sure you are inside the note_app folder when running the command below)</em>:</p><pre>python main.py</pre><p>Now visit <a href="http://127.0.0.1:5000/">http://127.0.0.1:5000/</a> in your browser.</p><p>You should now see the home page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fmofCQIxRcu99AHDr1Ka2Q.jpeg" /></figure><p>Our current app is not yet fully functional. In part 2, we will start adding the create a note page, note list page etc…</p><p>Update: Part 2 is now published: <a href="https://medium.com/python-flask-django-tutorials-and-tips/how-to-build-a-crud-application-using-flask-python-framework-part-2-6859b4730350">https://medium.com/python-flask-django-tutorials-and-tips/how-to-build-a-crud-application-using-flask-python-framework-part-2-6859b4730350</a></p><p>Want to get updated when I publish another tutorial? Follow me on Twitter: <a href="https://twitter.com/John200Ok">https://twitter.com/John200Ok</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a398b04c3c6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/python-flask-django-tutorials-and-tips/how-to-build-a-crud-application-using-flask-python-framework-3a398b04c3c6">How to build a CRUD application using Flask (Python Framework)</a> was originally published in <a href="https://medium.com/python-flask-django-tutorials-and-tips">Python, Flask, Django Tutorials and Tips</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>