<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dries De Decker</title>
    <description>The latest articles on Forem by Dries De Decker (@driesdd).</description>
    <link>https://forem.com/driesdd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F411692%2Fad082635-5936-4a11-ba3e-e757d8363877.jpeg</url>
      <title>Forem: Dries De Decker</title>
      <link>https://forem.com/driesdd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/driesdd"/>
    <language>en</language>
    <item>
      <title>Making your first dynamic site with Flask and Heroku</title>
      <dc:creator>Dries De Decker</dc:creator>
      <pubDate>Wed, 09 Dec 2020 08:30:01 +0000</pubDate>
      <link>https://forem.com/driesdd/making-your-first-dynamic-site-with-flask-and-heroku-3ph8</link>
      <guid>https://forem.com/driesdd/making-your-first-dynamic-site-with-flask-and-heroku-3ph8</guid>
      <description>&lt;p&gt;Since I just have my first dynamic website online, I wanted to share some experiences which may be useful to other junior web devs in the same position. You can see my site, where I published this article, &lt;a href="https://www.dries.page/website" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2rurbulxlh20l870vjn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2rurbulxlh20l870vjn8.png" alt="My screen while working on the site"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The challenge
&lt;/h2&gt;

&lt;p&gt;The easiest way to make your own website would probably be to use a template from SquareSpace or WordPress or something similar. For most people this might be the best option. But I’m assuming you are not most people, you are an aspiring web developer. You’re a nerd about staying in control of all the layout and scripts on your site, so you want to code it yourself.&lt;/p&gt;

&lt;p&gt;Now, the easiest way to make your own website would go something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start from a HTML boilerplate to give some structure to your HTML document.&lt;/li&gt;
&lt;li&gt;Keep CSS styling minimal and generic, by using a classless CSS microframework like water.css or sakura. You can download the framework to customize it if needed.&lt;/li&gt;
&lt;li&gt;Put the code on Github or Gitlab.&lt;/li&gt;
&lt;li&gt;Once the site works locally, use Github Pages, Gitlab pages or Netlify to host it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nice! You made a static site. The reason I did not go this route was because I have some more requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I don’t want to have to copy the code that’s the same on every page. In other words: I need a content management system that automatically updates all pages if I want to change the header or other generic elements on my site.&lt;/li&gt;
&lt;li&gt;I don’t want a static site. I want the site to run on a server, so I can play around with the fun stuff like web sockets, databases, and running secure server-side code. Most of that is not relevant right now but the groundwork needs to be there.&lt;/li&gt;
&lt;li&gt;I want to host the site on my own domain (&lt;a href="http://www.dries.page" rel="noopener noreferrer"&gt;www.dries.page&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;I want to customize the style of the site.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alright, now this is more of a challenge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flask
&lt;/h2&gt;

&lt;p&gt;To satisfy our first two requirements of having a site that reuses code and is dynamic, we will need a framework. Laravel, Symfony, Ruby on Rails, Spring Boot, Django… The best web framework depends on the programming language you like and the complexity of your site. Most frameworks use a Model View Controller (MVC) structure, and are modular enough that differences will only matter if your project gets large or very specific.&lt;/p&gt;

&lt;p&gt;Most frameworks also come with a bunch of premade folders and files which seem over-engineered for simple websites. They’re not that complex once you are used to them, but since I wanted to use the minimal needed complexity, I chose a Python ‘microframework’ called Flask. It allows me to get a site going with one small python file, and add all the HTML elements in a folder called ‘templates’.&lt;/p&gt;

&lt;p&gt;The folder structure could look something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

├── build.py
└── templates
    ├── articles
    │   ├── article1.html
    │   └── home.html
    └── layout.html


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Layout.html is the layout of any page on your site, with the article inserted in between block markers: &lt;code&gt;{% block content %}{% endblock %}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The pages then each need to ‘extend’ the layout using &lt;code&gt;{% extends 'layout.html' %}&lt;/code&gt; and wrap all the contents in a &lt;code&gt;{% block content %} content {% endblock %}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code of build.py would look something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#Import flask and the functions to redirect and render pages
from flask import Flask, redirect, render_template

#name of our app
app = Flask(__name__)

#an empty route (mysite.com) returns mysite.com/home. This is optional. 
#You could also do it the other way around.
@app.route('/')
def home():
    return redirect('/home')

#what to serve for a given page.
@app.route('/')
def article(page):
    return render_template('/articles/'+page+'.html')

app.run(debug=True)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;By default, Flask runs on &lt;code&gt;localhost:5000&lt;/code&gt; so you can easily test your site as you build it.&lt;/p&gt;

&lt;p&gt;That's it, we now have an extremely basic content management system that runs on a local server.&lt;/p&gt;

&lt;p&gt;The next step is to add a CSS framework. I'm using Tailwind, since I love how customizable it is without having to write additional CSS classes. For this project it was the best choice out of the frameworks I know, because it aligns with our goal of staying in control and keeping the final result simple (after purging). Ultimately, which framework you use is a matter of personal preference that hardly affects your workflow of making a dynamic site. However, it's hard to switch later on, so choose wisely.&lt;/p&gt;

&lt;p&gt;Next, we need to buy a domain. I have good experiences with namecheap.com. Their domains seemed a little cheaper overall, they have good documentation, reputable service and a clear interface for DNS configuration and such. While an email address is included it is redirected to your personal email address, unless you pay extra. However, it is easy and free to setup your site with a good SSL/TLS certificate from Let's Encrypt. This will come in handy later on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The server
&lt;/h2&gt;

&lt;p&gt;Now, we only have to host our site on our own domain. This took longer than I anticipated. It's not something I really learned during my training so I ran into a few hurdles.&lt;/p&gt;

&lt;p&gt;The first hurdle was hosting the site on a remote server, not via our own domain yet. Of all the options out there, in the end, only Heroku seems to offer a perpetually free server with all the required tools. I also had a decent experience with a provider called CloudSigma, but couldn't get everything up and running within my free trial period and I wasn't willing to pay because I was unsure if I would run into other unforeseen issues with them. However, in the end, Heroku has everything I needed.&lt;/p&gt;

&lt;p&gt;First of all, if you like Flask but your site doesn't really need a server or much of a back-end, you can take a big shortcut here using a module called Frozen-Flask. It will turn your flask site into a static site so you don't have to worry about Heroku and dyno's and environments and credit cards at all. For everyone else, make sure to oil your search engines.&lt;/p&gt;

&lt;p&gt;Something I didn't quite appreciate was that the server is in fact another machine than my own. Of course I knew this, but the implications are that it has its own operating system, its own hardware resources, its own applications, and all of this needs to be setup correctly. Heroku offers a dyno that runs the server process, a CLI which allows you to control and troubleshoot your site builds from your own terminal, integration with Github, a not too complicated process of creating the environment on which the app runs via a buildpack, and a big community. The documentation is sometimes a bit too brief but the options are very extensive. If you want to add a database from scratch, it might be worth picking up PostgreSQL since it can be added as a resource in Heroku. This makes it easier to have persistent data and manipulate your database remotely.&lt;/p&gt;

&lt;p&gt;Something else I needed to do was to use another command than the built-in app.run to serve my flask application, since it's not really made for production environments. I used a Flask module called 'waitress'. The end of my build.py script now looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

if __name__ == "__main__":
    app.debug = False
    port = int(os.environ.get('PORT', 33507))
    waitress.serve(app, port=port)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I also kept a separate file to still build locally using the built-in flask run function.&lt;/p&gt;

&lt;p&gt;Now for a little list that I wish someone had told me. To make sure that Flask and all its modules work on the server, I needed to add these files in my root directory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A procfile with a build command. I used &lt;code&gt;web: gunicorn build:app&lt;/code&gt;. A procfile is just a file without an extension, called PROCFILE, which contains commands that Heroku will run automatically when the app is uploaded.&lt;/li&gt;
&lt;li&gt;run.gunicorn.sh, which is the bash command referenced in the procfile. The contents in my case are &lt;code&gt;gunicorn -b :5000 --access-logfile - --error-logfile - build:app&lt;/code&gt; where &lt;code&gt;build&lt;/code&gt; is the name of my build file (build.py) and &lt;code&gt;app&lt;/code&gt; is the name of my app, in the file.&lt;/li&gt;
&lt;li&gt;runtime.txt, which specifies which python version to run. For example, in my case the contents of the file are simply &lt;code&gt;python-3.8.5&lt;/code&gt;. This file is not strictly required but it's useful to avoid issues related to having a different python version locally and on the server.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;requirements.txt. This is a file which you may see in any python project, it lists the requiremed modules and their version. For our project, the dependencies are:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Flask==0.11.0
Jinja2==2.7
Werkzeug==0.9.1
waitress==1.4.4
gunicorn==19.6.0
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
In case you are wondering, Werkzeug is a library that Flask uses, while Jinja2 is the templating language.

Our project structure now looks like this:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;├── build.py&lt;br&gt;
├── buildlocal.py&lt;br&gt;
├── PROCFILE&lt;br&gt;
├── run.gunicorn.sh&lt;br&gt;
├── runtime.txt&lt;br&gt;
├── requirements.txt&lt;br&gt;
├── templates&lt;br&gt;
│   ├── articles&lt;br&gt;
│   │   ├── article1.html&lt;br&gt;
│   │   └── home.html&lt;br&gt;
│   └── layout.html&lt;br&gt;
└── static&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
The static folder contains all CSS, javascript, images and other static resources.

With the site hosted as a Heroku app, I still had to connect it to my personal domain. This requires verifying your Heroku account with a visa- or mastercard, but it's free. Then, I had some security issues - adding a security certificate to Heroku is not free, and my own domain didn't trust the Heroku site so the user was greeted with a notification that the site is untrusted. Fortunately, I found out how to set it up right in the Advanced DNS settings:

![Screenshot of the DNS configuration showing a redirect URL to dries.herokuapp.com](https://dev-to-uploads.s3.amazonaws.com/i/74cu0capjmpzi3z3g6ou.png)

Makes sense if you see it like that, but in my mind a redirect from my domain to the heroku domain would make the user see heroku in the address bar. Not the case! We have added our personal domain in Heroku, and so Heroku will serve to our domain, using the security certificate from our own domain. No extra certificate on Heroku needed.

That was the final piece of the puzzle. As you can see, the website is still very basic but I reached my initial goals. If you read this far, thank you! I hope this article can be useful to my fellow junior devs.

Dries out!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>heroku</category>
    </item>
    <item>
      <title>Git basics</title>
      <dc:creator>Dries De Decker</dc:creator>
      <pubDate>Thu, 18 Jun 2020 12:06:43 +0000</pubDate>
      <link>https://forem.com/driesdd/git-basics-15fd</link>
      <guid>https://forem.com/driesdd/git-basics-15fd</guid>
      <description>&lt;p&gt;A simple guide on editing a repository offline using &lt;code&gt;terminal commands&lt;/code&gt;. &lt;code&gt;These&lt;/code&gt; are the commands you need.&lt;br&gt;
This assumes you have git, github, a repository (or repo) which you can access in github, and a linux or mac terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. moving to the right folder
&lt;/h2&gt;

&lt;p&gt;a) Type &lt;code&gt;pwd&lt;/code&gt; in the terminal and press enter to see where you are.&lt;/p&gt;

&lt;p&gt;b) &lt;code&gt;ls&lt;/code&gt; or &lt;code&gt;dir&lt;/code&gt; to show the things in the current directory&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ls -l&lt;/code&gt; to list information on the files and folders&lt;/p&gt;

&lt;p&gt;c) Use &lt;code&gt;cd&lt;/code&gt; to change directory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd&lt;/code&gt; to go to the home directory&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd docs&lt;/code&gt; to go to  a specific folder named docs&lt;/p&gt;

&lt;p&gt;(repeat until you are in the folder you need to be)&lt;/p&gt;

&lt;p&gt;you can also use &lt;code&gt;cd ..&lt;/code&gt; to move one directory up and &lt;code&gt;cd /..&lt;/code&gt; to move to the top directory&lt;/p&gt;

&lt;p&gt;or you could simply navigate to the right folder with your file browser, right click it, and select 'Open in Terminal' or the equivalent option&lt;/p&gt;

&lt;h2&gt;
  
  
  2. cloning a repository (download it to your current directory to work in it)
&lt;/h2&gt;

&lt;p&gt;a) go to the github page for the repository&lt;/p&gt;

&lt;p&gt;b) click the green clone or download button and copy the LINK (it ends with .git)&lt;/p&gt;

&lt;p&gt;c) We now start work in git. All git commands start with &lt;code&gt;git&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;git clone LINK&lt;/code&gt; to clone the online repository (this is called a remote repo) to the folder you're in (now it's a local repo)&lt;/p&gt;

&lt;p&gt;d) you are not working in the repo yet. You still need to go there using &lt;code&gt;cd NAME&lt;/code&gt; where NAME is the name of the repo&lt;/p&gt;

&lt;p&gt;e) you are in the repo now!&lt;/p&gt;

&lt;h2&gt;
  
  
  3. opening a branch and editing it
&lt;/h2&gt;

&lt;p&gt;a) use &lt;code&gt;git branch -a&lt;/code&gt; or &lt;code&gt;git branch -av&lt;/code&gt; to show all branches. The branch you are now in will be green or white.&lt;/p&gt;

&lt;p&gt;b) if you need to create a new branch just use &lt;code&gt;git branch NEWBRANCHNAME&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;c) &lt;code&gt;git checkout BRANCH&lt;/code&gt; to start working in the branch named BRANCH&lt;/p&gt;

&lt;p&gt;d) &lt;code&gt;ls&lt;/code&gt; or &lt;code&gt;dir&lt;/code&gt; to show the files in the repo to know which files you can open&lt;/p&gt;

&lt;p&gt;e) &lt;code&gt;open FILENAME&lt;/code&gt; to open the file named &lt;code&gt;FILENAME&lt;/code&gt;. If &lt;code&gt;open&lt;/code&gt; doesn't work you can also open it manually by going to the folder in your file browser. Text files can also be edited within the terminal using vim or nano but I don't recommend it.&lt;/p&gt;

&lt;p&gt;you are now editing a file!&lt;/p&gt;

&lt;h2&gt;
  
  
  4. push your changes to the repo
&lt;/h2&gt;

&lt;p&gt;a) once you are done improving the file, save it.&lt;/p&gt;

&lt;p&gt;b) then do &lt;code&gt;git add&lt;/code&gt; to move your file to your staging area&lt;/p&gt;

&lt;p&gt;You can also use &lt;code&gt;git add A&lt;/code&gt; to move ALL files in the currrent folder to the staging area. Useful when you work on multiple files.&lt;/p&gt;

&lt;p&gt;c) then do &lt;code&gt;git commit&lt;/code&gt; to commit the file to your local repository&lt;/p&gt;

&lt;p&gt;you can also do step b and c at the same time by doing &lt;code&gt;git commit -a&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;d) then do &lt;code&gt;git push&lt;/code&gt; or &lt;code&gt;git push origin&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(&lt;code&gt;origin&lt;/code&gt; is the URL of the online repo, if you want to push to another repo than where you pulled it from you need to replace &lt;code&gt;origin&lt;/code&gt;. In that case you would probably get an error though which you might be able to solve by adding &lt;code&gt;-f&lt;/code&gt; to force your changes down the throat of the unwilling repo)&lt;/p&gt;

&lt;p&gt;e) you'll need to enter your github username and password. If you're new to all of this you may be surprised that you don't see anything while you type your password, but that's normal.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. merging changes from branch A to branch B
&lt;/h2&gt;

&lt;p&gt;a) go to branch B (use &lt;code&gt;checkout B&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;b) do &lt;code&gt;git merge A&lt;/code&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>terminal</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
