<?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[Stories by Miguel Valdes on Medium]]></title>
        <description><![CDATA[Stories by Miguel Valdes on Medium]]></description>
        <link>https://medium.com/@rorix?source=rss-3f2b02ccf6e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hP9Qz6hAHyLJAuROvw9E2g.jpeg</url>
            <title>Stories by Miguel Valdes on Medium</title>
            <link>https://medium.com/@rorix?source=rss-3f2b02ccf6e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 20:27:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rorix/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Track the hours you work for the man with Python and Flask!]]></title>
            <link>https://medium.com/@rorix/track-the-hours-you-work-for-the-man-with-python-and-flask-5375f9850704?source=rss-3f2b02ccf6e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5375f9850704</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[tracker]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Miguel Valdes]]></dc:creator>
            <pubDate>Tue, 12 Dec 2017 13:14:31 GMT</pubDate>
            <atom:updated>2017-12-12T13:14:31.165Z</atom:updated>
            <content:encoded><![CDATA[<p>As a person that works in support 24/7 I tend to work over the weekend and of course those hours get billed to the client, at first I used a little post-it near my computer and it helped, but eventually I started adding more hours to the same post-its and it all became a mess, so it was decided to just build a small webapp that would run in my raspberry pi so i could simply put the hours and store them into a DB.</p><p>That way i can look back and see how much of a slave i am. So let’s get started.</p><p><strong>What is flask?</strong></p><p>Flask is a micro-framework for the python language, the moment you install it you get the basics needed for you to create a web application. Unlike some other frameworks like Django that include everything you may or may not need like DB connections, serialization, etc (known as batteries included). Flask only provides a very small core of functions, so you actually get to pick which modules you may or may not need.</p><p>For more information on flask please visit the (link)[<a href="http://flask.pocoo.org/">http://flask.pocoo.org/</a>]. Most flask applications follow a very simple structure.</p><pre>├── app.py<br>├── overtime.db<br>├── README.md<br>├── requirements.txt<br>├── static<br>│   └── style.css<br>└── templates<br>    └── index.html</pre><ol><li>app.py — our application code (due to the simplicity of it we are using a single file, if you app is bigger, its better to separate it into modules/files/folders.</li><li>overtime.db — our sqlite3 database that was manually created.</li><li>requirements.txt — all of the dependencies needed for this project.</li><li>templates — where flask will look for all of your html files, this is a critical folder you must have otherwise flask might not be able to find your files unless of course you specify it in the configurations.</li><li>static — where you could host all of your static files such as css, js, images, etc.</li></ol><p>Now, let the coding begin. Code can be found on (Github Expenses)[<a href="https://github.com/RorixRebel/flask-overtime">https://github.com/RorixRebel/flask-overtime</a>]</p><p>First we need to import and create the instances.</p><pre><strong>from</strong> <strong>flask</strong> <strong>import</strong> Flask, render_template, request, redirect, url_for, flash<br><strong>import</strong> <strong>sqlite3</strong></pre><pre>app = Flask(__name__)</pre><p>Now since we will be using sqlite3 to store the data, we will make use of some supporting functions to insert, delete, remove data. All of these use (context managers)[<a href="https://rorixrebel.github.io/context-managers-101.html">https://rorixrebel.github.io/context-managers-101.html</a>] to manager the enter and exit actions.</p><pre><em># Supporting functions</em><br><strong>def</strong> db_create():<br>   <strong>with</strong> sqlite3.connect(&quot;overtime.db&quot;) <strong>as</strong> connection:<br>      cursor = connection.cursor()<br>      <strong>try</strong>:<br>         cursor.execute(&quot;CREATE TABLE overtime_list (id INTEGER PRIMARY KEY, Date TEXT, Start TEXT, End TEXT)&quot;)<br>      <strong>except</strong> Exception:<br>         <strong>pass</strong><br></pre><pre><strong>def</strong> ot_put(data):<br>  <strong>with</strong> sqlite3.connect(&quot;overtime.db&quot;) <strong>as</strong> connection:<br>      cursor = connection.cursor()<br>      cursor.execute(&quot;INSERT INTO overtime_list (Date, Start, End)VALUES(?, ?, ?)&quot;, data)<br></pre><pre><strong>def</strong> ot_get():<br>  <strong>with</strong> sqlite3.connect(&quot;overtime.db&quot;) <strong>as</strong> connection:<br>      cursor = connection.cursor()<br>      ot_list = cursor.execute(&quot;SELECT * from overtime_list&quot;)<br>      <strong>return</strong> ot_list<br></pre><pre><strong>def</strong> ot_remove(id):<br>  <strong>with</strong> sqlite3.connect(&quot;overtime.db&quot;) <strong>as</strong> connection:<br>    cursor = connection.cursor()<br>    <strong>try</strong>:<br>      cursor.execute(&quot;DELETE FROM overtime_list WHERE id = ?&quot;, (id,))<br>    <strong>except</strong> Exception:<br>      <strong>pass</strong></pre><p>Most of these are pretty easy to understand as we are using raw SQL to insert/remove the records. There is one quick function that creates the database called db_create(). Ideally you should only run it once or make it part of your overall application. Or if you want to create the schema/table manually via sqlite3 that’s OK as well.</p><p><strong>How does Flask sends the info to the web app?</strong></p><p>Flask uses JINJA2 Templating Engine — <a href="http://jinja.pocoo.org/">http://jinja.pocoo.org/</a> This allows you to pass content in a “pythonic” way, meaning, with simply passing variables to your templates which are typically HTML files you can display data on your web apps using {{% expression % }}}. Let’s see how we do that within our routes.</p><p>Now, we create <strong>routes</strong> that basically are endpoints in your application that depending on the method (GET, POST, DELETE, etc.) will react differently. In my case i only need to have the one route that shows me everything and a “hidden” route that takes care of removing items if needed.</p><pre><em># Routes</em><br><strong>@app</strong>.route(&quot;/&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])<br><strong>def</strong> index():<br>    data = []<br>    <strong>if</strong> request.method == &quot;POST&quot;:<br>        date = request.form.get(&quot;date&quot;)<br>        start = request.form.get(&quot;start&quot;)<br>        end = request.form.get(&quot;end&quot;)<br>        <strong>for</strong> i <strong>in</strong> (date, start, end):<br>            data.append(i)<br>        ot_put(data)<br>        flash(&quot;Record added&quot;, &quot;success&quot;)<br>        <strong>return</strong> redirect(url_for(&quot;index&quot;))<br>    ot_list = ot_get()<br>    <strong>return</strong> render_template(&quot;index.html&quot;,<br>                           data=data,<br>                           ot_list=ot_list)</pre><p>Note how we are passing 2 variables to our “index.html” file here. — Variable data sends a list of all the items collected from the form. - Variable ot_list executes the ot_get function to query the database and extract all rows in our sqlite3 db.</p><p>It may look a bit repetitive since we are saying that data = data which is how we from python/flask name our variable to use in our HTML code. You will see how that makes sense in a bit.</p><p>Finally, we have our delete route that simply removes records from the DB. Note how this route only accepts POSTmethods, so its somewhat restricted on how you interact with it.</p><p>You could hide it or make it more secure but for our purposes, this works just fine.</p><pre><strong>@app</strong>.route(&quot;/delete&quot;, methods=[&quot;POST&quot;])<br><strong>def</strong> db_delete():<br>    <strong>if</strong> request.method == &quot;POST&quot;:<br>        id = request.form[&quot;id&quot;]<br>        ot_remove(id)<br>        flash(&quot;Record removed&quot;, &quot;danger&quot;)<br>        <strong>return</strong> redirect(url_for(&quot;index&quot;))</pre><p>The last bit is to tell the file to actually run the application once you call it. For DEV purposes, its good if you use the flag debug = True. In prod you should get rid of it.</p><pre><strong>if</strong> __name__ == &quot;__main__&quot;:<br>    app.run(debug=<strong>True</strong>)</pre><p>Once you have the file saved, we now need to work on the HTML part that will actually display the items we will use to interact with our flask application. Will not into much detail for the HTML so i will just highlight some critical things that flask uses to interact with the web component.</p><p>Our entire HTML file: I’ll be separating some snippets that make use of the JINJA templates we talked before.</p><pre>&lt;!doctype html&gt;</pre><pre>&lt;<strong>head</strong>&gt;<br>    &lt;<strong>title</strong>&gt;Overtime Tracker&lt;/<strong>title</strong>&gt;<br>    &lt;<strong>link</strong> rel=&quot;stylesheet&quot; href=&quot;<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css</a>&quot;<br>          integrity=&quot;sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u&quot; crossorigin=&quot;anonymous&quot;&gt;<br>    &lt;<strong>link</strong> rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;{{ url_for(&#39;static&#39;, filename=&#39;style.css&#39;) }}&quot;&gt;<br>&lt;/<strong>head</strong>&gt;</pre><pre>&lt;<strong>h1</strong> class=&quot;centered&quot;&gt;Overtime Tracker&lt;/<strong>h1</strong>&gt;<br>&lt;<strong>h2</strong> class=&quot;centered&quot;&gt;Track the hours you spend working for the man!&lt;/<strong>h2</strong>&gt;</pre><pre>&lt;<strong>body</strong>&gt;<br>&lt;<strong>br</strong>&gt;<br>&lt;<strong>div</strong> class=&quot;container&quot;&gt;</pre><p>This is a snippet actually taken from the flask documentation so you can flash and put messages on your app to let users know if something either was a success (adding a record) or if it went wrong (record not found or recor deleted).</p><p>The position where you put this snipper is where the messages will pop up. <strong>NOTE</strong>: Every time you do a with or if, foryou need to end it using {{% end “expression” %}}.</p><pre>{% <strong>with</strong> messages = get_flashed_messages(with_categories=true) %}<br>  {% <strong>if</strong> messages %}<br>  {% <strong>for</strong> category, message <strong>in</strong> messages %}<br>  <strong>&lt;div</strong> class=&quot;alert alert-{{ category }} centered&quot;<strong>&gt;<br>        </strong>{{ message }}<br>  <strong>&lt;/div&gt;</strong><br>  {% <strong>endfor</strong> %}<br>  {% <strong>endif</strong> %}<br>  {% <strong>endwith</strong> %}</pre><pre>    {% <strong>if</strong> error %}<br>        <strong>&lt;div</strong> class=&#39;alert alert-danger&#39;<strong>&gt;</strong>{{error}}<strong>&lt;/div&gt;</strong><br>    {% <strong>endif</strong> %}</pre><pre>    {% <strong>if</strong> msg %}<br>        <strong>&lt;div</strong> class=&#39;alert alert-success&#39;<strong>&gt;</strong>{{msg}}<strong>&lt;/div&gt;</strong><br>    {% <strong>endif</strong> %}</pre><p>Creating the table…</p><pre><strong>&lt;div</strong> class=&quot;row&quot;<strong>&gt;</strong><br>  <strong>&lt;div</strong> class=&quot;col-md-6 centered btn-group-lg btn-group-vertical&quot;<strong>&gt;</strong><br>     <strong>&lt;form</strong> method=&quot;POST&quot; action=&quot;/&quot; id=&quot;user-form&quot;<strong>&gt;</strong><br>       <strong>&lt;div</strong> class=&quot;form-group&quot;<strong>&gt;</strong><br>         <strong>&lt;label</strong> for=&quot;date&quot;<strong>&gt;</strong>Date<strong>&lt;/label&gt;</strong><br>         <strong>&lt;input</strong> type=&quot;date&quot; class=&quot;form-control&quot; id=&quot;date&quot; name=&quot;date&quot;<strong>&gt;&lt;br&gt;</strong><br>         <strong>&lt;label</strong> for=&quot;start&quot;<strong>&gt;</strong>Start Time<strong>&lt;/label&gt;</strong><br>         <strong>&lt;input</strong> type=&quot;time&quot; class=&quot;form-control&quot; id=&quot;start&quot; name=&quot;start&quot;<strong>&gt;&lt;br&gt;</strong><br>         <strong>&lt;label</strong> for=&quot;end&quot;<strong>&gt;</strong>End Time<strong>&lt;/label&gt;</strong><br>         <strong>&lt;input</strong> type=&quot;time&quot; class=&quot;form-control&quot; id=&quot;end&quot; name=&quot;end&quot;<strong>&gt;&lt;br&gt;</strong><br>       <strong>&lt;/div&gt;</strong><br>       <strong>&lt;button</strong> type=&quot;submit&quot; class=&quot;btn btn-primary&quot;<strong>&gt;</strong>Submit<strong>&lt;/button&gt;</strong><br>     <strong>&lt;/form&gt;</strong><br><strong>&lt;/div&gt;</strong></pre><p>In here we take the values we get from the flask app encapsulated in the ot_list variable and display them. First we check if the list contains values using an “if list”.</p><pre>{% <strong>if</strong> ot_list %}<br> <strong>&lt;div</strong> class=&quot;centered&quot;<strong>&gt;</strong><br>   <strong>&lt;h2&gt;</strong> Existing Overtime Data <strong>&lt;/h2&gt;</strong><br>      <strong>&lt;table</strong> class=&quot;table-striped table-bordered text-center&quot; align=&quot;center&quot;<strong>&gt;</strong><br>            <strong>&lt;tr&gt;</strong><br>                <strong>&lt;th&gt;</strong>Date<strong>&lt;/th&gt;</strong><br>                <strong>&lt;th&gt;</strong>Start Time<strong>&lt;/th&gt;</strong><br>                <strong>&lt;th&gt;</strong>End Time<strong>&lt;/th&gt;</strong><br>                <strong>&lt;th&gt;</strong>Remove<strong>&lt;/th&gt;</strong><br>            <strong>&lt;/tr&gt;</strong></pre><p>Once we validate the contents of the list we simply loop and put the values in the table.</p><pre>{% <strong>for</strong> i <strong>in</strong> ot_list %}<br>  <strong>&lt;tr&gt;</strong><br>    <strong>&lt;td&gt;</strong>{{ i[1] }}<strong>&lt;/td&gt;</strong><br>    <strong>&lt;td&gt;</strong>{{ i[2] }}<strong>&lt;/td&gt;</strong><br>    <strong>&lt;td&gt;</strong>{{ i[3] }}<strong>&lt;/td&gt;</strong><br>    <strong>&lt;td&gt;</strong><br>      <strong>&lt;form</strong> action=&#39;/delete&#39; method=&#39;POST&#39;<strong>&gt;</strong><br>      <strong>&lt;input</strong> type=&#39;hidden&#39; value={{i[0]}} name=&#39;id&#39;/<strong>&gt;</strong><br>      <strong>&lt;input</strong> type=&#39;submit&#39; class=&#39;btn btn-danger&#39; value=&#39;Remove&#39;<strong>/&gt;</strong><br>      <strong>&lt;/form&gt;</strong><br>    <strong>&lt;/td&gt;</strong><br>  <strong>&lt;/tr&gt;<br></strong>{% <strong>endfor</strong> %}<br></pre><p>Once you have the structure and your files ready we can simply run it using python app.py and it will start on <a href="http://localhost:5000/">http://localhost:5000</a>.</p><p>At the end this is how it should look.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v06M5OzclPife9Ack3y1NA.png" /></figure><p>The intent of the post isn’t to show you how to build apps for flask as I have left A LOT of things unexplained and I’m using the bare minimal. This was mostly a little example of how I’m using my knowledge in python to facilitate my own personal life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5375f9850704" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Upgrade your PC with Reddit + Python and Flask]]></title>
            <link>https://medium.com/@rorix/upgrade-your-pc-with-reddit-python-and-flask-4696f11d591e?source=rss-3f2b02ccf6e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4696f11d591e</guid>
            <category><![CDATA[reddit]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[python-flask]]></category>
            <dc:creator><![CDATA[Miguel Valdes]]></dc:creator>
            <pubDate>Wed, 06 Dec 2017 21:56:36 GMT</pubDate>
            <atom:updated>2017-12-06T21:59:14.795Z</atom:updated>
            <content:encoded><![CDATA[<p>The crazy shopping season is over for now, so a lot of people will start selling a lot of their used hardware to recover from the expenses made during this past Black Friday/Cyber Monday, in my case as a computer junkie (#PCMasterRace FTW) I always like to have my PC up to date and the best way is to buy cheap gently used hardware. Now Reddit has this magical place called “Hardwareswap” where people sell their parts/rigs among other things like cameras, cell phones, etc.</p><p>So because of this I decided to create a simple flask app that would search in that subreddit, not to say that the built in function doesn’t work but sometimes its just hard to navigate and see the relevant information you want or if you want to sort it, becomes a bit convoluted… so instead my application presents the results in a cleaner way in my opinion.</p><p>Alright so first here is the finished product.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yAdvIG6X84kf4ROop42qZA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7oJUwr0iJ4EE26Zv3gTr_g.png" /></figure><h3>Requirements</h3><ul><li>praw</li><li>flask</li></ul><p>You must create an app to obtain a client and secret keys. Follow — <a href="https://praw.readthedocs.io/en/latest/getting_started/authentication.html">https://praw.readthedocs.io/en/latest/getting_started/authentication.html</a></p><p>As an extra component i’m using gunicorn so i can focus on the editor with the live reload running in the background as Atom tends to either stop the server when i click on something i shouldn’t or if i run the file twice i get an error about it already running polluting my console output and just distracting me, either way in the long run for deploying it is always a good idea to use gunicorn to handle your requests.</p><p>If you just want to test the application then go straight to the setup.</p><h3>Code</h3><p>For this particular project we are using a single page and a single route so the code is pretty easy to follow. Additionally we keep the reddit praw component in a separate file so it’s easier to maintain.</p><h3>app.py</h3><pre>from flask <strong>import</strong> <strong>Flask</strong>, <strong>render_template</strong>, <strong>request</strong><br><strong>from</strong> <strong>reddit_data</strong> <strong>import</strong> get_from_reddit<br><br><em># Flask app</em><br>app = Flask(__name__)<br>app.config.from_pyfile(&quot;config.py&quot;)</pre><p>We do our regular imports and instantiate the application as well as define where to take the configuration parameters from.</p><pre><strong>@app</strong>.route(&#39;/&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<br><strong>def</strong> index():<br>    <strong>if</strong> request.method == &quot;POST&quot;:<br>        query = request.form.get(&#39;query&#39;)<br>        data = get_from_reddit(query)<br>        <strong>if</strong> data:<br>            <strong>return</strong> render_template(&quot;index.html&quot;, data=data)<br>        <strong>return</strong> render_template(&quot;index.html&quot;, msg=&quot;Item not found&quot;)<br>    <strong>return</strong> render_template(&quot;index.html&quot;)</pre><pre><strong>if</strong> __name__ == &quot;__main__&quot;:<br>    app.run()</pre><p>Then we create our unique route ‘/’ that accepts POST and GET requests. In here depending on the type of request we either return the default HTML file or we run the reddit methods to extract the data. We also have a condition to check if we received data back from reddit and based on that we return something to the template so the user can see what went down.</p><p>Finally we just do the boilerplate to run the application.</p><h3>reddit_data.py</h3><p>For this application to work we create 2 helper functions.</p><ol><li>bot_login = Will be our reddit instance needed for praw to operate properly, here is where we feed the credentials we exported earlier.</li><li>get_from_reddit = We run a query against our desired subreddit with specific parameters and create a structure that will be passed to the HTML.</li></ol><p>Now the code.</p><pre><strong>import</strong> <strong>praw</strong><br><strong>import</strong> <strong>config</strong><br><strong>import</strong> <strong>re</strong></pre><pre><strong>def</strong> bot_login():<br>    reddit = praw.Reddit(username=config.reddit_username,<br>                         password=config.reddit_password,<br>                         client_id=config.client_id,<br>                         client_secret=config.client_secret,<br>                         user_agent=&quot;HWSearch App v1.0&quot;)<br>    <strong>return</strong> reddit</pre><p>We create the reddit instance and return it so we can use it in the other function. If this isn’t clear i would suggest you review the praw documentation as creating the instance is the first thing you need to do in order to interact with the API.</p><p><strong>NOTE</strong>: We also imported the re library since we will use it later on. If this is the first time you see this library, i would recommend checking my other <a href="https://rorixrebel.github.io/regex-101.html">post</a> on regex basics.</p><pre><strong>def</strong> get_from_reddit(item):<br>    results = {}<br>    reddit = bot_login()<br>    reddit_posts = reddit.subreddit(&#39;hardwareswap&#39;).search(<br>        item,<br>        sort=&#39;new&#39;,<br>        time_filter=&#39;week&#39;)<br>    <strong>for</strong> item <strong>in</strong> reddit_posts:<br>        <strong>if</strong> item.link_flair_text == &#39;Closed&#39; <strong>or</strong> <strong>not</strong> item.title.startswith(&#39;CAN&#39;):<br>            <strong>continue</strong><br>        location = re.findall(&#39;USA\s?\-?\s?\w+&#39;, item.title)<br>        results[item.title] = {&#39;location&#39;: location,<br>                               &#39;type&#39;: item.link_flair_text,<br>                               &#39;text&#39;: item.selftext,<br>                               &#39;url&#39;: item.url}<br>    <strong>return</strong> results</pre><p>Now the “meaty” part of the code that does the data extraction and manipulation. We first create an empty dictionary to hold the structure we want. Then the instance is created which enables us to use the methods defined in the API, in here we pass our specifics like the subreddit, the time_filter and how we want to sort the results, these are saved in a reddit_instance_object which can be iterated to retrieve specifics.</p><p>Before creating our structure we check if the post is for the US (sorry Canada but shipping would kill me). And we also just want to see open posts.</p><p>Once we pass the validation and have the object we iterate over it and assemble our dictionary with the data we need (title, flair, text, url, etc.). The one other tricky piece here is the regex search on the title. Since most posts follow certain rules we simply create a pattern that indicates we are looking for:</p><pre>The words USA followed by a potential(?) blank space(\s), followed by a potential dash(\-?) , another space(\s?) and one or more(+) word characters(\w).</pre><p>Finally we return this structure so it can be used by the route.</p><h3>index.html</h3><p>For this file i won’t go into much detail but i will just highlight the critical parts that interact with our python app file.</p><pre><strong>&lt;form</strong> class=&quot;&quot; action=&quot;{{ url_for(&#39;index&#39;)}}&quot; method=&quot;post&quot;<strong>&gt;</strong><br>    <strong>&lt;label&gt;</strong>What are you looking for?<strong>&lt;/label&gt;</strong><br>    <strong>&lt;input</strong> type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;query&quot; required=&quot;required&quot;<strong>&gt;</strong><br>    <strong>&lt;p</strong> class=&quot;help-block&quot;<strong>&gt;</strong>Enter an item name.<strong>&lt;/p&gt;</strong><br>    <strong>&lt;button</strong> class=&quot;btn btn-success&quot; type=&quot;submit&quot; name=&quot;button&quot;<strong>&gt;</strong>Search<strong>&lt;/button&gt;</strong><br><strong>&lt;/form&gt;</strong></pre><p>First we have the form that sends the post request to our index route with the data entered by the user. Giving it a “name=” parameter makes the retrieval process easier as we can just do a request.form.get(&#39;name&#39;) and use it for our processing.</p><pre>{% <strong>if</strong> msg %}<br><strong>&lt;span</strong> class=&quot;msg&quot;<strong>&gt;</strong> {{ msg }} <strong>&lt;/span&gt;</strong><br>{% <strong>endif</strong> %}</pre><p>If no data is found in the subreddit we print the message in the HTML. Beautiful Jinja2 templating engine.</p><pre>{% <strong>if</strong> data %}<br><strong>&lt;table</strong> data-toggle=&quot;table&quot; data-sort-order=&quot;desc&quot; data-sorter=&quot;htmlSorter&quot;<strong>&gt;</strong><br>.<br>.<br>.<br>{% <strong>for</strong> key, value <strong>in</strong> data.items()%}<br><strong>&lt;tr&gt;</strong><br>    <strong>&lt;td&gt;</strong><br>        {{ key | safe }}<br>    <strong>&lt;/td&gt;</strong><br>    <strong>&lt;td&gt;</strong><br>        {{ value[&#39;location&#39;] | safe }}<br>    <strong>&lt;/td&gt;</strong><br>    <strong>&lt;td&gt;</strong><br>        {{ value[&#39;type&#39;] | safe }}<br>    <strong>&lt;/td&gt;</strong><br>    <strong>&lt;td&gt;</strong><br>        {{ value[&#39;text&#39;] | safe }}<br>    <strong>&lt;/td&gt;</strong><br>.<br>.<br>.<br>{% <strong>endfor</strong> %}<br>{% <strong>endif</strong> %}</pre><p>In this last part we first check if we have any data to display, based on that we iterate over the structure we created earlier and just display it in the rows we defined. Since the structure is basically a dictionary we just give it the field and key name.</p><p>It is very important that you close your if, for, with, block statements in Jinja.</p><h3>Setup</h3><p>Repo — <a href="https://github.com/RorixRebel/flask-reddithwswap">https://github.com/RorixRebel/flask-reddithwswap</a></p><p>If you already cloned the repo, all you need to do is export your reddit credentials with the following names.</p><pre>export REDDIT_USERNAME=&quot;&quot;<br>export REDDIT_PASSWORD=&quot;&quot;<br>export REDDIT_CID=&quot;&quot;<br>export REDDIT_CSECRET=&quot;&quot;</pre><p>Or you can modify the config.py file to rename the values to whatever you want.</p><p>Now, if you are using gunicorn you simply run:</p><pre>gunicorn app:app</pre><p>if using the default WSGI server on flask then:</p><pre>python app.py</pre><p>The application is also live running in Heroku:</p><pre><a href="https://polar-garden-47020.herokuapp.com/">https://polar-garden-47020.herokuapp.com/</a></pre><p>That is it folks. If you have any questions, suggestions or features you would like to see on the app please let me know in the comments below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4696f11d591e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>