<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Salvador Villalon Jr - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Salvador Villalon Jr - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 30 May 2026 16:30:46 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/salvadorvillalon/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ What are Pre-Rendering and Hydration in Web Development? A Deep Dive for Devs ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever wondered how Frameworks like Next.js, Gatsby.js, and Remix work? These frameworks use the concepts of Pre-rendering and Hydration, which represent significant advancements in the history of web development. These frameworks leverage the... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-are-pre-rendering-and-hydration-in-web-dev/</link>
                <guid isPermaLink="false">6703ee6b07ec01d7c3c14651</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Next.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ hydration ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pre rendering ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Full Stack Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Salvador Villalon Jr ]]>
                </dc:creator>
                <pubDate>Mon, 07 Oct 2024 14:21:31 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1727985850590/fdb7eb16-52bd-41ec-8e1a-e4fb9068a535.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever wondered how Frameworks like <a target="_blank" href="https://nextjs.org/"><strong>Next.js</strong></a>, <a target="_blank" href="https://www.gatsbyjs.com/"><strong>Gatsby.js</strong></a>, and <a target="_blank" href="https://remix.run/"><strong>Remix</strong></a> work?</p>
<p>These frameworks use the concepts of Pre-rendering and Hydration, which represent significant advancements in the history of web development.</p>
<p>These frameworks leverage these concepts to create toolchains that build efficient web applications. In this article, we’ll discuss Pre-rendering and Hydration and why they are important features to use when building single-page applications.</p>
<p>To understand these concepts, we need to explore why they were created and what problem they are trying to solve. Let's take a look at the beginning of web applications.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-web-development-in-the-past-traditional-server-side-rendering">Web Development in the Past: Traditional Server-Side Rendering</a></p>
<ul>
<li><a class="post-section-overview" href="#heading-the-downside-of-traditional-server-side-rendering">The Downside of Traditional Server-Side Rendering</a></li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-traditional-server-side-rendering-vs-single-page-applications">Traditional Server-Side Rendering vs. Single-Page Applications</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-a-single-page-application-spa">What is a Single Page Application (SPA)?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-single-page-application-flow">Single Page Application Flow</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-entering-a-new-world-with-pre-rendering-and-hydration">Entering a New World with Pre-Rendering and Hydration</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-why-is-pre-rendering-important">Why is Pre-Rendering Important?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-server-side-rendering">What is Server Side Rendering?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-static-site-generation">What is Static Site Generation (SSG)?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-hydration">What is Hydration?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-reconciliation">What is Reconciliation?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-pre-rendering-and-hydration-in-action">Pre-rendering and Hydration in Action</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-a-mental-model-for-hydration">A Mental Model for Hydration</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-potential-errors-when-working-with-pre-rendering-and-hydration">Potential Errors when working with Pre-rendering and Hydration Frameworks</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-how-does-it-relate-to-frameworks-like-gatsbyjs-nextjs-and-remix">How does it relate to frameworks like Gatsby.js, Next.js, and Remix?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-moving-forward">Moving Forward</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-moving-forward">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-web-development-in-the-past-traditional-server-side-rendering">Web Development in the Past: Traditional Server-Side Rendering</h2>
<p>Back in the days of traditional server-side rendering, rendering and interactivity were separated. We used server-side languages like <a target="_blank" href="https://nodejs.org/en"><strong>Node.js</strong></a>, <a target="_blank" href="https://www.php.net/"><strong>PHP</strong></a>, <a target="_blank" href="https://www.java.com/en/"><strong>Java</strong></a>, and <a target="_blank" href="https://rubyonrails.org/"><strong>Ruby on Rails</strong></a>.</p>
<p>Within our servers, we created <strong>views</strong> using templating languages like <a target="_blank" href="https://en.wikipedia.org/wiki/Jakarta_Server_Pages"><strong>JSP</strong></a> and <a target="_blank" href="https://ejs.co/"><strong>EJS</strong></a>. Views are HTML pages, and you could inject JavaScript or Java inside them to add functionality, dynamic data retrieved from database queries, and interactive segments with languages like <a target="_blank" href="https://jquery.com/"><strong>JQuery</strong></a>.</p>
<h3 id="heading-the-downside-of-traditional-server-side-rendering">The Downside of Traditional Server-Side Rendering</h3>
<ol>
<li><strong>Performance Issues</strong></li>
</ol>
<ul>
<li><p>A request to the server needed to be made every time the user requested a page!</p>
<ul>
<li><p>This meant there would be a full-page reload.</p>
</li>
<li><p>Complex queries could result in slower speeds.</p>
</li>
</ul>
</li>
</ul>
<ol start="2">
<li><strong>Scalability Challenges</strong></li>
</ol>
<ul>
<li><p><strong>Global Reach</strong>: A <strong>Dynamic CDN</strong> was needed to cache your dynamic files. CDNs are better suited for static content, but companies like Cloudflare created <a target="_blank" href="https://www.cloudflare.com/products/cloudflare-workers/"><strong>Cloudflare-Workers</strong></a> to help with the process.</p>
</li>
<li><p><strong>Upscaling Servers</strong>: If more users started using the application, there would be an increase in demand on the server. You might have needed to invest more in resources such as scaling up by adding more servers.</p>
</li>
</ul>
<ol start="3">
<li><strong>Duplicate Logic</strong></li>
</ol>
<ul>
<li>You may have duplicate code. For example, if you were trying to validate form fields, you'd have to validate in both the EJS file and your API endpoint.</li>
</ul>
<p>Let's look at the code snippet below to see an example of this duplicate Logic:</p>
<p><strong>Code in EJS:</strong></p>
<pre><code class="lang-javascript">&lt;form action=<span class="hljs-string">'/submit-form'</span> method=<span class="hljs-string">'POST'</span> id=<span class="hljs-string">"myForm"</span>&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"email"</span>&gt;</span>Email:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span></span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"email"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"email"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"email"</span> /&gt;</span></span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
&lt;/form&gt;

<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
    <span class="hljs-built_in">document</span>
        .getElementById(<span class="hljs-string">'myForm'</span>)
        .addEventListener(<span class="hljs-string">'submit'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
            <span class="hljs-keyword">const</span> email = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'email'</span>).value;

            <span class="hljs-keyword">if</span> (!email.includes(<span class="hljs-string">'@'</span>)) {
                alert(<span class="hljs-string">'Please enter a valid email.'</span>);
                event.preventDefault();
            }
        });
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<p><strong>Code in Express.js:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">"express"</span>;
<span class="hljs-keyword">const</span> app = express();
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);
<span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;

<span class="hljs-comment">// To received form data</span>
app.use(
  express.urlencoded({
    <span class="hljs-attr">extended</span>: <span class="hljs-literal">true</span>,
  }),
);

<span class="hljs-comment">// view engine setup. Need a folder called views</span>
app.set(<span class="hljs-string">"views"</span>, path.join(__dirname, <span class="hljs-string">"views"</span>));
app.set(<span class="hljs-string">"view engine"</span>, <span class="hljs-string">"ejs"</span>);

app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
  <span class="hljs-comment">// To render the view</span>
  res.render(<span class="hljs-string">"index"</span>, { <span class="hljs-attr">errors</span>: <span class="hljs-literal">null</span> });
});

app.post(<span class="hljs-string">"/submit-form"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> email = req.body.email;

  <span class="hljs-keyword">if</span> (!email.includes(<span class="hljs-string">"@"</span>)) {
    res.status(<span class="hljs-number">400</span>).send(<span class="hljs-string">"Invalid email."</span>);
    <span class="hljs-keyword">return</span>;
  }
  <span class="hljs-comment">// Proceed with form processing</span>
});

app.listen(port, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Sandbox listening on port <span class="hljs-subst">${port}</span>`</span>);
});
</code></pre>
<p>Traditional Server-Side Rendering had significant downsides, but the introduction of <strong>single-page applications</strong> marked a new era in web development.</p>
<h2 id="heading-traditional-server-side-rendering-vs-single-page-applications">Traditional Server-Side Rendering vs. Single-Page Applications</h2>
<h3 id="heading-what-is-a-single-page-application-spa">What is a Single Page Application (SPA)?</h3>
<blockquote>
<p><strong><em>A Single-page application (SPA) is a web app implementation that loads only a single web document and then updates the body content of that single document via JavaScript APIs such as Fetch when different content is to be shown. Allows users to use websites without loading whole new pages from the server. (</em></strong><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Glossary/SPA"><strong><em>Source: MDN</em></strong></a><strong><em>)</em></strong></p>
</blockquote>
<p>A popular way to implement SPA is by using React. React lets you create fast applications and simplifies updating the UI more easily than DOM manipulation methods.</p>
<p>It offers several advantages:</p>
<ul>
<li><p><strong>Improved User Experience</strong></p>
<ul>
<li><p>An SPA loads a single HTML file and dynamically updates the content as the user interacts with it. All of this is done without a full page reload.</p>
</li>
<li><p>An SPA can update the state of the UI easily and provide instant feedback to users based on actions taken on the app.</p>
</li>
</ul>
</li>
<li><p><strong>Reduced Server Load</strong></p>
<ul>
<li>Most of the work is done by the browser. This reduces the load on the server!</li>
</ul>
</li>
<li><p><strong>Better Scalability</strong></p>
<ul>
<li><p>The browser is now doing most of the work. We can now deploy dedicated servers focused on serving data via APIs. We can easily scale horizontally. We have the option of using servers or serverless functions like <a target="_blank" href="https://aws.amazon.com/lambda/"><strong>AWS lambda</strong></a>.</p>
</li>
<li><p>An SPA can be hosted on a static CDN like <a target="_blank" href="https://docs.netlify.com/platform/what-is-netlify/"><strong>Netlify</strong></a>.</p>
</li>
</ul>
</li>
</ul>
<p>With the addition of toolchains like <a target="_blank" href="https://vitejs.dev/"><strong>Vite</strong></a> and <a target="_blank" href="https://create-react-app.dev/"><strong>Create React App</strong></a> to automate the setup of a modern JavaScript application, engineers no longer had to worry about configuring Webpack manually.</p>
<p>There are some downsides to implementing SPAs. The big one is that it relies on the browser to load all the JavaScript and HTML for us. This means that on mobile devices and for people with slow internet, users may experience delays in seeing the page. Let’s examine the flow to explain this:</p>
<h3 id="heading-single-page-application-flow">Single Page Application Flow</h3>
<blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725392287290/04dba828-9903-4ca6-bf8e-0c0d875d587b.png?auto=compress,format&amp;format=webp" alt="Single Page Application React Flow. (Source: How NextJS REALLY Works: https://youtu.be/d2yNsZd5PMs?si=RmnywZJEAuurseQm)" width="854" height="487" loading="lazy"></p>
</blockquote>
<p>Several steps are needed for the users to finally see the HTML page.</p>
<p>First, the browser fetches the HTML. This initial HTML will be blank and incorrect. Why? Because the content comes from JavaScript. This means that it takes time for the browser to fetch JavaScript, load it, and execute it. Since the initial HTML is wrong, web crawlers and search engines will not find relevant content on the website and skip it.</p>
<p>Take a look at the below GIF. Here JavaScript is disabled on the Chrome Developer Tools. The website fails to load without JavaScript. If JavaScript is enabled but the internet connection is slow, users may see a blank page for an extended period.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725392877108/d5a2eb1c-f784-4879-b02f-8c340d405c9c.gif?auto=format,compress&amp;gif-q=60&amp;format=webm" alt="Single Page Application JavaScript Disabled Test." width="1916" height="908" loading="lazy"></p>
<p>This was a big problem. This led to Web Development evolving into <strong>the Pre-Rendering era.</strong></p>
<h2 id="heading-entering-a-new-world-with-pre-rendering-and-hydration">Entering a New World with Pre-Rendering and Hydration</h2>
<h3 id="heading-why-is-pre-rendering-important">Why is Pre-Rendering Important?</h3>
<p>People realized that we could generate the HTML in advance. It could be generated from our server or at build time, depending on the methods used.</p>
<p>Pre-rendering can be done in two ways - <a target="_blank" href="https://nextjs.org/docs/pages/building-your-application/rendering/server-side-rendering"><strong>Server Side Rendering (SSR)</strong></a> or <a target="_blank" href="https://nextjs.org/docs/pages/building-your-application/rendering/static-site-generation"><strong>Static Site Generation (SSG)</strong></a></p>
<h3 id="heading-what-is-server-side-rendering">What is Server Side Rendering?</h3>
<p>React components are rendered on the server, and the resulting HTML is sent to the browser. This can improve SEO and initial load times. <strong>The rendering process occurs on each page request.</strong></p>
<h3 id="heading-what-is-static-site-generation-ssg">What is Static Site Generation (SSG)?</h3>
<p><strong>Static HTML pages are generated at build time.</strong> These pages can be served quickly without requiring a server to render them on the fly.</p>
<p>Either method is beneficial! <strong>Now the HTML that the user receives will be correct.</strong> They will see a page with content instead of a blank page as seen using Vite or Create React App.</p>
<p>But there is a problem: the HTML the user receives is not interactive. They cannot click on it or submit a form. How can we add interactivity to our app? By Hydrating properly🚰 🌊!</p>
<h3 id="heading-what-is-hydration">What is Hydration?</h3>
<p><a target="_blank" href="https://react.dev/reference/react-dom/hydrate#hydrating-server-rendered-html"><strong>Hydration</strong></a> is what adds interactivity to our app. It loads the JavaScript that makes our app interactive.</p>
<blockquote>
<p><strong><em>In React, "hydration" is how React "attaches" to existing HTML that was already rendered by React in a server environment. During hydration, React will attempt to attach event listeners to the existing markup and take over rendering the app on the client. (</em></strong><a target="_blank" href="https://react.dev/reference/react-dom/hydrate#hydrating-server-rendered-html"><strong><em>Source: React Docs</em></strong></a><strong><em>)</em></strong></p>
</blockquote>
<p>Let's see what the flow looks like for an app that uses Pre-Rendering and Hydration:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725392958385/eaee78cb-736c-4d49-8fb7-c9cb098d3652.png?auto=compress,format&amp;format=webp" alt="Pre-rendering flow." width="851" height="499" loading="lazy"></p>
<h3 id="heading-what-is-reconciliation"><strong>What is Reconciliation?</strong></h3>
<blockquote>
<p><strong><em>Reconciliation is the process by which React determines the most efficient way to update the UI in response to changes in data or component hierarchy. (Source:</em></strong> <a target="_blank" href="https://www.educative.io/answers/what-is-the-concept-of-reconciliation-in-react"><strong><em>What is the difference between virtual and real DOM (React)?</em></strong></a><strong><em>)</em></strong></p>
</blockquote>
<p>Reconciliation is when React figures out how to update the UI in response to changes in data or components hierarchy.</p>
<p>When components are rendered, a Virtual DOM is created. If there are changes in state or props, then a new Virtual DOM is created. React then uses its diff algorithm to compare the new Virtual DOM with the previous Virtual DOM to check for changes. This is <strong>reconciliation</strong>.</p>
<p>Based on the changes found, <strong>React is not going to update the entire UI</strong>. Instead, it will <strong>select which elements need to be updated.</strong> This <a target="_blank" href="https://www.educative.io/answers/what-is-the-concept-of-reconciliation-in-react"><strong>article</strong></a> helped me understand Reconciliation.</p>
<h3 id="heading-pre-rendering-and-hydration-in-action"><strong>Pre-rendering and Hydration in Action</strong></h3>
<p>During the Pre-rendering and Hydration flow, first, the user will see HTML with the correct content.</p>
<p>Then Hydration kicks in and loads JavaScript to give interactivity to the application.</p>
<p>Let's simulate the process of what happens if the Hydration process takes a long time (due to a slow internet connection) or if the user has JavaScript disabled.</p>
<p>Here is a gif where I disable JavaScript on my Portfolio. I created my portfolio using <a target="_blank" href="https://www.gatsbyjs.com/"><strong>Gatsby</strong></a> a static-site-generation framework (it also has server-side rendering capabilities):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725554638438/698fd854-e828-44cd-996a-e01b154803d1.gif?auto=format,compress&amp;gif-q=60&amp;format=webm" alt="Portfolio JavaScript Disabled Test." width="1916" height="908" loading="lazy"></p>
<p>Even though there is no JavaScript, I can still see content on my portfolio. That’s because the user received <strong>pre-render HTML</strong>! You can see that I cannot click on the <strong>dropdown menu items or the buttons</strong> that say About Me, Projects, and Experience. That’s because the JavaScript did not load so the user cannot interact with it.</p>
<h3 id="heading-a-mental-model-for-hydration"><strong>A Mental Model for Hydration</strong></h3>
<p><a target="_blank" href="https://www.joshwcomeau.com/"><strong>Josh Comeau</strong></a> created a cool mental model for Hydration. Josh calls it the <strong>“Two Pass Rendering.”</strong></p>
<blockquote>
<p><strong><em>The first pass, at compile-time, produces all of the static non-personal content and leaves holes where the dynamic content will go. Then, after the React app has mounted on the user's device, a second pass stamps in all the dynamic bits that depend on the client state. (Source:</em></strong> <a target="_blank" href="https://www.joshwcomeau.com/react/the-perils-of-rehydration/#mental-models-13"><strong><em>The Perils of Hydration</em></strong></a><strong><em>)</em></strong></p>
</blockquote>
<p>To Summarize:</p>
<ol>
<li><p><strong>The First Pass</strong>: the user sees the pre-render HTML. It contains static content, but it is missing the dynamic content.</p>
</li>
<li><p><strong>The Second Pass:</strong> JavaScript starts loading and fills in the missing dynamic pieces that depend on the client state.</p>
</li>
</ol>
<h3 id="heading-potential-errors-when-working-with-pre-rendering-and-hydration-frameworks"><strong>Potential Errors When Working with Pre-rendering and Hydration Frameworks</strong></h3>
<p>When working with frameworks like Next.js, the server will return static pre-render HTML, and then Hydration occurs which loads JavaScript.</p>
<p>But we must be careful when working with dynamic data and client-only properties. For example, take a look at this code:</p>
<h4 id="heading-dynamic-data-error">Dynamic Data Error</h4>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">HydrationErrors</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hydration Errors<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Today date in milliseconds is {new Date().getTime()}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>Here, the server will generate HTML with a timestamp in milliseconds. For example: <code>1724869161034</code>. The Hydration process begins, and then the client loads the HTML. Time has passed and the timestamp is different, it is now <code>172486193750</code>! This scenario causes the following error:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725555403697/ebb398ec-cd58-4b9a-aa63-c43a9b511eeb.png?auto=compress,format&amp;format=webp" alt="Mismatch text content from server and client hydration error." width="977" height="788" loading="lazy"></p>
<p>This happens because the <code>getTime()</code> function will generate a different timestamp.</p>
<p>This means that the server and client generate different HTML. The Network Tab shows us the server's response. It is a different HTML from what the client loads.</p>
<p><strong>The server's response below:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725555472118/d76c7ad0-92ab-4b77-92ea-b3d9d6a3b4fe.png?auto=compress,format&amp;format=webp" alt="Different server html generated." width="904" height="836" loading="lazy"></p>
<p><strong>The client's response below:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725555518535/af27855d-d362-44e3-8007-b01fde1d2455.png?auto=compress,format&amp;format=webp" alt="Different client html generated." width="1624" height="853" loading="lazy"></p>
<p><strong>To Fix the Error:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">HydrationErrors</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [date, setDate] = useState&lt;number&gt;();

  useEffect(<span class="hljs-function">() =&gt;</span> {
    setDate(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime());
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hydration Errors<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Today date in milliseconds is {date}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>You can use the <code>useEffect</code> hook. Why would this work? Because the HTML that the server and client render will contain an empty <code>date</code> state variable.</p>
<p>Once the component mounts, the <code>useEffect</code> activates and adds the dynamic data from the state variable or you can use the <code>suppressHydrationWarning</code> flag and set it to true.</p>
<pre><code class="lang-javascript">  &lt;p suppressHydrationWarning={<span class="hljs-literal">true</span>}&gt;Today date <span class="hljs-keyword">in</span> milliseconds is {date}&lt;/p&gt;
</code></pre>
<h4 id="heading-using-client-only-properties-error">Using Client-Only Properties Error</h4>
<p>Remember you cannot use <code>window</code> or <code>localStorage</code>. They do not exist on the server. Take the following example:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">HydrationErrors</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        {typeof window !== "undefined" &amp;&amp; <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This p tag will appear<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<p>Here, the server returns HTML with an empty <code>&lt;div&gt;</code> tag, but the client loads HTML that includes the <code>&lt;p&gt;</code> tag. This creates a <strong>HYDRATION ERROR!</strong></p>
<p>This is the error that you get:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725555844562/443c435c-308a-4422-b184-b2f321672519.png?auto=compress,format&amp;format=webp" alt="Cannot use client-side properties hydration error." width="952" height="778" loading="lazy"></p>
<p>The Network Tab shows us the server's response. It is an empty <code>&lt;div&gt;</code> tag.</p>
<p><strong>The server's response below:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725555958286/aeeaa522-3266-4b60-856d-d0cd3bf4d9c9.png?auto=compress,format&amp;format=webp" alt="Different server html generated." width="908" height="850" loading="lazy"></p>
<p>But the client loads HTML that says "This p tag will appear".</p>
<p><strong>The client's response below:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725556187527/173bf82a-1754-4835-9921-adea0f2e864a.png?auto=compress,format&amp;format=webp" alt="Different client html generated." width="1618" height="835" loading="lazy"></p>
<p>This demo was inspired by <a target="_blank" href="https://www.youtube.com/@deeecode"><strong>Deeecode The Web</strong></a> in <a target="_blank" href="https://youtu.be/7UOyuEHYmSE?si=Ql8z5D_pAryvTyFf"><strong>Why Do HYDRATION ERRORS Exist? And how to solve them</strong></a>. He gives a great explanation of why Hydration Errors occur. I recommend watching it!</p>
<h2 id="heading-how-does-it-relate-to-frameworks-like-gatsbyjs-nextjs-and-remix">How Does it Relate to Frameworks like Gatsby.js, Next.js, and Remix?</h2>
<p>Everything we have discussed is what all these frameworks focus on.</p>
<p>Static-Site Generation and Server-Side Rendering can be implemented using Gatsby.js, Next.js, and Remix. They focus on creating a pre-render HTML ready for the user to see, then initiate Hydration to add interactivity to the app.</p>
<p>Gatsby.js, Next.js, and Remix do not replace the concept of single-page applications – they add to the process. Take a look at this flow:</p>
<blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725556321232/7d8629fd-b528-4cd7-9bc2-7de0bf1ab630.png?auto=compress,format&amp;format=webp" alt="Pre-rendering and Single Page Application combined flow. (Source: How NextJS REALLY Works)" width="1164" height="500" loading="lazy"></p>
</blockquote>
<p>It is adding to the current SPA flow! If you did not have Pre-rendering, the process starts where the pink box begins, with an incomplete HTML.</p>
<h2 id="heading-moving-forward">Moving Forward</h2>
<p>This article is meant as an introduction to pre-rendering and hydration.</p>
<p>Next.js first implemented these concepts with the <a target="_blank" href="https://nextjs.org/docs/pages">Pages Router</a>. Pages Router was great and introduced the world to functions like <code>getServerSideProps</code>, <code>getStaticPaths</code>, and <code>getStaticProps</code> to implement Static-Site Generation and Server-Side Rendering.</p>
<p>These implementations had their pros and cons. For example, Josh W Comeau mentioned that with <code>getServerSideProps</code><strong>:</strong></p>
<blockquote>
<ol>
<li><p>This strategy only works at the route level, for components at the very top of the tree. We can't do this in any component.</p>
</li>
<li><p>Each meta-framework came up with its own approach. Next.js has one approach, Gatsby has another, Remix has yet another. It hasn't been standardized.</p>
</li>
<li><p>All of our React components will <em>always</em> hydrate on the client, even when there's no need for them to do so.</p>
<p> <a target="_blank" href="https://www.joshwcomeau.com/react/server-components/">(Source: Making Sense of React Server Components)</a></p>
</li>
</ol>
</blockquote>
<p>The React team saw this too and created a new paradigm called <a target="_blank" href="https://react.dev/reference/rsc/server-components">React Server Components (RSC)</a>. To implement RSC, the Vercel team created the <a target="_blank" href="https://nextjs.org/docs/app">App Router</a>. App Router still uses the concepts of pre-rendering and hydration, but it no longer uses <code>getStaticProps</code>, <code>getStaticPaths</code>, and <code>getServerSideProps</code>. It uses RSC and other great App Router features to implement better web applications. I recommend taking a look at App Router when you get a chance.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Thanks for reading this far 😃!</p>
<p>I learned a lot from writing this article. I started this research because I used Gatsby to create my <a target="_blank" href="https://salvador-villalon.netlify.app/"><strong>portfolio version 4</strong></a> and Next.js in my job, but I did not understand the concepts behind these frameworks and why they were created.</p>
<p>I made a web application to demonstrate the topics covered in the article.</p>
<ul>
<li><p>Application: <a target="_blank" href="https://the-nextjs-pagesrouter-guide.vercel.app/"><strong>https://the-nextjs-pagesrouter-guide.vercel.app/</strong></a></p>
</li>
<li><p>GitHub Repo: <a target="_blank" href="https://github.com/salvillalon45/the-nextjs-pagesrouter-guide"><strong>https://github.com/salvillalon45/the-nextjs-pagesrouter-guide</strong></a></p>
</li>
</ul>
<p>In the GitHub repo you can find the code snippets for the following:</p>
<ul>
<li><p>A page implementing getStaticProps and getStaticPaths</p>
</li>
<li><p>A page implementing getStaticProps</p>
</li>
<li><p>A page implementing getServerSideProps with Client Side fetching</p>
</li>
<li><p>A page to demonstrate Hydration Errors</p>
</li>
<li><p>Using the API directory to implement our own API routes</p>
</li>
</ul>
<h3 id="heading-resources">Resources</h3>
<p>Here are some key learning resources I used to write this article in case you want to dig in even deeper:</p>
<ul>
<li><p><a target="_blank" href="https://youtu.be/R-BKadZWYnQ?si=imNFJL36knSPdF7S"><strong>What is Hydration</strong></a><strong>?</strong> by Builder</p>
</li>
<li><p><a target="_blank" href="https://youtu.be/87i0pejrULw?si=e179x9x2KkaR8AxL"><strong>What Exactly is REACT HYDRATION? And why does it matter?</strong></a> by Deeecode The Web</p>
</li>
<li><p><a target="_blank" href="https://youtu.be/d2yNsZd5PMs?si=UZYKHUrajdXQd1y4&amp;t=1"><strong>How NextJS REALLY Works</strong></a> by Theo Browne</p>
</li>
<li><p><a target="_blank" href="https://youtu.be/xfX8nVpaycU?si=ZV-r2piDoWhLAKMi"><strong>Next.js - GetServerSideProps vs GetStaticProps</strong></a> by Morado Web Development</p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to build a web application using Flask and deploy it to the cloud ]]>
                </title>
                <description>
                    <![CDATA[ Introduction In each section, I will show pieces of code for you to follow along. All the code used in the tutorial is available in this GitHub Repository. What is HTTP and What Does it Have to do with Flask? HTTP is the protocol for websites. The in... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-a-web-application-using-flask-and-deploy-it-to-the-cloud-3551c985e492/</link>
                <guid isPermaLink="false">66c3500f912fb29c4f156c31</guid>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ startup ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Salvador Villalon Jr ]]>
                </dc:creator>
                <pubDate>Tue, 28 Aug 2018 15:04:48 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*OrCZB4PxwGqppjkoIMzCaA.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <h3 id="heading-introduction"><strong>Introduction</strong></h3>
<p>In each section, I will show pieces of code for you to follow along. All the code used in the tutorial is available in this <a target="_blank" href="https://github.com/salvillalon45/SPGISummer2018-FlaskTutorial">GitHub Repository</a>.</p>
<h2 id="heading-what-is-http-and-what-does-it-have-to-do-with-flask">What is HTTP and What Does it Have to do with Flask?</h2>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">HTTP</a> is the protocol for websites. The internet uses it to interact and communicate with computers and servers. Let me give you an example of how you use it everyday.</p>
<p>When you type the name of a website in the address bar of your browser and you hit enter. What happens is that an HTTP request has been sent to a server.</p>
<p>For example, when I go to my address bar and type google.com, then hit enter, an HTTP request is sent to a Google Server. The Google Server receives the request and needs to figure how to interpret that request. The Google Server sends back an HTTP response that contains the information that my web browser receives. Then it displays what you asked for on a page in the browser.</p>
<h3 id="heading-how-is-flask-involved">How is Flask involved?</h3>
<p>We will write code that will take care of the server side processing. Our code will receive requests. It will figure out what those requests are dealing with and what they are asking. It will also figure out what response to send to the user.</p>
<p>To do all this we will use Flask.</p>
<h2 id="heading-what-is-flask">What is Flask?</h2>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1SnE5y1jhzqsSoFvgFyWc4mRLXX-iuG2DPtm" alt="Image" width="721" height="282" loading="lazy"></p>
<p>_[Flask (A Python Microframework)](http://flask.pocoo.org/" rel="noopener" target="<em>blank" title=")</em></p>
<p>It makes the process of designing a web application simpler. Flask lets us focus on what the <strong>users are requesting and what sort of response to give back.</strong></p>
<p>Learn more about <a target="_blank" href="https://en.wikipedia.org/wiki/Microframework">micro frameworks</a>.</p>
<h2 id="heading-how-does-a-flask-app-work">How Does a Flask App Work?</h2>
<p>The code lets us run a basic web application that we can serve, as if it were a website.</p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask

app = Flask(__name__)

<span class="hljs-meta">@app.route("/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span>():</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello, World!"</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    app.run(debug=<span class="hljs-literal">True</span>)
</code></pre>
<p>This piece of code is stored in our main.py.</p>
<p><strong>Line 1:</strong> Here we are importing the Flask module and creating a Flask web server from the Flask module.</p>
<p><strong>Line 3: name means this current file</strong>. In this case, it will be main.py. This current file will represent my web application.</p>
<p>We are creating an instance of the Flask class and calling it app. Here we are creating a new web application.</p>
<p><strong>Line 5:</strong> It represents the default page. For example, if I go to a website such as “google.com/” with nothing after the slash. Then this will be the default page of Google.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/rl8hovE8cNuaTMh6X8S8y4LLYoAVTJ5uvSMh" alt="Image" width="800" height="306" loading="lazy"></p>
<p><em>This is what the @app.route(“/”) will represent</em></p>
<p><strong>Line 6–7</strong>: When the user goes to my website and they go to the default page (nothing after the slash), then the function below will get activated.</p>
<p><strong>Line 9:</strong> When you run your Python script, Python assigns the name “<strong>main</strong>” to the script when executed.</p>
<p>If we import another script, the <strong>if statement will prevent other scripts from running.</strong> When we run main.py, it will change its name to <strong>main</strong> and only then will that if statement activate.</p>
<p><strong>Line 10:</strong> This will run the application. Having <code>debug=True</code> allows possible Python errors to appear on the web page. This will help us trace the errors.</p>
<h3 id="heading-lets-try-running-mainpy"><strong>Let’s Try Running main.py</strong></h3>
<p>In your Terminal or Command Prompt go to the folder that contains your main.py. Then do <code>**py main.py**</code> or <code>**python main.py**</code><strong>.</strong> In your terminal or command prompt you should see this output.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/QynmMfSb7CoZkeuyC38ZLrTuA5eHtTkSxb6n" alt="Image" width="540" height="157" loading="lazy"></p>
<p>The important part is where it says <code>Running on http://127.0.0.1:5000/</code>.</p>
<p>127.0.0.1 means this local computer. If you do not know the meaning of this (like I didn’t when I started — <a target="_blank" href="https://whatismyipaddress.com/localhost">this article is really helpful</a>), the main idea is that 127.0.0.1 and localhost refer to this local computer.</p>
<p>Go to that address and you should see the following:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/3TazBO699jNj7C88MyFbf-jlqseyWRPL5N4X" alt="Image" width="335" height="151" loading="lazy"></p>
<p><em>Congrats! You made a website with Flask!</em></p>
<h4 id="heading-more-fun-with-flask"><strong>More Fun with Flask</strong></h4>
<p>Earlier you saw what happened when we ran main.py with one route which was app.route(“/”).</p>
<p>Let’s add more routes so you can see the difference.</p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask

app = Flask(__name__)

<span class="hljs-meta">@app.route("/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span>():</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello, World!"</span>

<span class="hljs-meta">@app.route("/salvador")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">salvador</span>():</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello, Salvador"</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    app.run(debug=<span class="hljs-literal">True</span>)
</code></pre>
<p>In <strong>lines 9–11</strong>. we added a new route, this time to <strong>/salvador.</strong></p>
<p>Now run the main.py again and go to <a target="_blank" href="http://localhost:5000/salvador">http://localhost:5000/salvador</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/63Ib3PYx1He2Y0pRiH9As7OivEc-dBmsVdYE" alt="Image" width="325" height="134" loading="lazy"></p>
<p>So far we have been returning text. Let’s make our website look nicer by adding HTML and CSS.</p>
<h2 id="heading-html-css-and-virtual-environments">HTML, CSS, and Virtual Environments</h2>
<h3 id="heading-html-and-templates-in-flask">HTML and Templates in Flask</h3>
<p>First create a new HTML file. I called mine <strong>home.html.</strong></p>
<p>Here is some code to get you started.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">"ltr"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Flask Tutorial<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> My First Try Using Flask <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Flask is Fun <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h4 id="heading-important-point-to-remember"><strong>Important Point To Remember</strong></h4>
<p>The Flask Framework looks for HTML files in a folder called <strong>templates.</strong> You <strong>need to create a templates</strong> folder and put all your HTML files in there.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/Ggzs2MzQiYmno0hkvLCbdj2-rWl4gzqkjZil" alt="Image" width="127" height="95" loading="lazy"></p>
<p><em>Remember to always keep the main.py outside of your templates folder</em></p>
<p>Now we need to change our main.py so that we can view the HTML file we created.</p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, render_template      

app = Flask(__name__)

<span class="hljs-meta">@app.route("/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span>():</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"home.html"</span>)

<span class="hljs-meta">@app.route("/salvador")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">salvador</span>():</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello, Salvador"</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    app.run(debug=<span class="hljs-literal">True</span>)
  We made two new changes
</code></pre>
<p><strong>Line 1:</strong> We imported <code>render_template()</code> method from the flask framework. <code>render_template()</code> looks for a template (HTML file) in the templates folder. Then it will render the template for which you ask. Learn more about <a target="_blank" href="http://flask.pocoo.org/docs/0.12/quickstart/#rendering-templates">render_templates() function</a>.</p>
<p><strong>Line 7:</strong> We change the return so that now it returns <code>render_template(“home.html”)</code>. This will let us view our HTML file.</p>
<p>Now visit your localhost and see the changes: <a target="_blank" href="http://localhost:5000/">http://localhost:5000/</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/YmVgj3pCcduDB6T5UBabraK-glrVPNGhc4R9" alt="Image" width="363" height="145" loading="lazy"></p>
<h3 id="heading-lets-add-more-pages"><strong>Let’s add more pages</strong></h3>
<p>Let’s create an <strong>about.html</strong> inside the <strong>templates folder.</strong></p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">"ltr"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>About Flask<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> About Flask <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Flask is a micro web framework written in Python.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Applications that use the Flask framework include Pinterest,
      LinkedIn, and the community web page for Flask itself.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Let’s make a change similar to what we did before to our main.py.</p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, render_template

app = Flask(__name__)

<span class="hljs-meta">@app.route("/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span>():</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"home.html"</span>)

<span class="hljs-meta">@app.route("/about)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">about</span>():</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"about.html"</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    app.run(debug=<span class="hljs-literal">True</span>)
</code></pre>
<p>We made three new changes:</p>
<p><strong>Line 9:</strong> Change the route to<code>"/about**"**</code><strong>.</strong></p>
<p><strong>Line 10:</strong> Change the function so it is now <code>def about():</code></p>
<p><strong>Line 11:</strong> Change the return so that now it returns <code>render_template("about.html")</code>.</p>
<p>Now see the changes: <a target="_blank" href="http://localhost:5000/about">http://localhost:5000/about</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/O1Oo1GXYO2LHnXRiz8-VmKuvpywABTV7MuVn" alt="Image" width="765" height="180" loading="lazy"></p>
<h3 id="heading-lets-connect-both-pages-with-a-navigation">Let’s Connect Both Pages with a Navigation</h3>
<p>To connect both pages we can have a navigation menu on the top. We can use Flask to make the process of creating a navigation menu easier.</p>
<p>First, let’s create a <strong>template.html.</strong> This <strong>template.html</strong> will serve as a parent template. Our two child templates will inherit code from it.</p>
<pre><code class="lang-html"> <span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">"ltr"</span>&gt;</span>
 <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Flask Parent Template<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{{ url_for('static',     filename='css/template.css') }}"</span>&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
 <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"logo"</span>&gt;</span>First Web App<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"menu"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{{ url_for('home') }}"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{{ url_for('about') }}"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>

    {% block content %}
    {% endblock %}

 <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><strong>Line 13–14:</strong> We use the function called <code>url_for()</code><strong>.</strong> It accepts the name of the function as an argument. Right now we gave it the name of the function. More information on <a target="_blank" href="http://flask.pocoo.org/docs/0.12/quickstart/#url-building"><strong>url_for() function</strong></a><strong>.</strong></p>
<p>The two lines with the curly brackets will be <strong>replaced by the content of home.html and about.html.</strong> This will depend on the URL in which the user is browsing.</p>
<p>These changes allow the child pages (home.html and about.html) to connect to the parent (template.html). This allows us to not have to <strong>copy the code for the navigation menu in the about.html and home.html.</strong></p>
<p>Content of about.html:</p>
<pre><code class="lang-javascript">&lt;!DOCTYPE html&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">"ltr"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>About Flask<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    {% extends "template.html" %}
    {% block content %}

    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> About Flask <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Flask is a micro web framework written in Python.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Applications that use the Flask framework include Pinterest,
      LinkedIn, and the community web page for Flask itself.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

    {% endblock %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></span>
</code></pre>
<p>Content of home.html:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">"ltr"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Flask Tutorial<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    {% extends "template.html" %}
    {% block content %}

    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> My First Try Using Flask <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Flask is Fun <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

    {% endblock %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Let’s try adding some CSS.</p>
<h2 id="heading-adding-css-to-our-website">Adding CSS to Our Website</h2>
<h3 id="heading-an-important-note-to-remember">An important note to remember</h3>
<p>In the same way as we created a folder called <strong>templates</strong> to store all our HTML templates, we need a folder called <strong>static</strong>.</p>
<p>In <strong>static</strong>, we will store our CSS, JavaScript, images, and other necessary files. That is why it is important that you should create a <strong>CSS</strong> <strong>folder to store your stylesheets.</strong> After you do this, your project folder should look like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ui4uCQ2KhjEaJ1JQ71XgXwNIs5TkzPuaGU8W" alt="Image" width="164" height="213" loading="lazy"></p>
<h3 id="heading-linking-our-css-with-our-html-file">Linking our CSS with our HTML file</h3>
<p>Our template.html is the one that links all pages. We can insert the code here and it will be applicable to all child pages.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">"ltr"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Flask Parent Template<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{{ url_for('static',    filename='css/template.css') }}"</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"logo"</span>&gt;</span>First Web App<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"menu"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{{ url_for('home') }}"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"{{ url_for('about') }}"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>

{% block content %}
{% endblock %}

 <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><strong>Line 7:</strong> Here we are giving the path to where the template.css is located.</p>
<p>Now see the changes: <a target="_blank" href="http://localhost:5000/about">http://localhost:5000/about</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/kvI3GKRQwhuu2O2giyPZBvu2am63So7OLH32" alt="Image" width="429" height="250" loading="lazy"></p>
<h2 id="heading-moving-forward-with-flask-and-virtualenv">Moving Forward with Flask and virtualenv</h2>
<p>Now that you are familiar with using Flask, you may start using it in your future projects. One thing to always do is use virtualenv.</p>
<h3 id="heading-why-use-virtualenv">Why use virtualenv?</h3>
<p>You may use Python for others projects besides web-development.</p>
<p>Your projects might have different versions of Python installed, different dependencies and packages.</p>
<p>We use virtualenv to create an isolated environment for your Python project. This means that each project can have its own dependencies regardless of what dependencies every other project has.</p>
<h3 id="heading-getting-started-with-virtualenv">Getting started with virtualenv</h3>
<p>First, run this command on your command prompt or terminal:</p>
<pre><code class="lang-bash">pip install virtualenv
</code></pre>
<p>Second, do the following:</p>
<pre><code class="lang-bash">virtualenv “name of virtual environment”
</code></pre>
<p>Here you can give a name to the environment. I usually give it a name of virtual. It will look like this: <code>virtualenv virtual</code><strong>.</strong></p>
<p>After setting up virtual environment, check your project folder. It should look like this. The virtual environment needs to be created in the <strong>same directory where your app files are located.</strong></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/nlsgTQVp9ZrBHudAD4yKWF7tywO5fsaiYvHq" alt="Image" width="165" height="126" loading="lazy"></p>
<p><em>How the directory looks like</em></p>
<h3 id="heading-activating-the-virtual-environment">Activating the virtual environment</h3>
<p>Now go to your terminal or command prompt. Go to the directory that contains the file called <strong>activate.</strong> The file called <strong>activate</strong> is found inside a folder called <strong>Scripts for Windows</strong> and <strong>bin for OS X and Linux.</strong></p>
<p>For OS X and Linux Environment:</p>
<pre><code class="lang-bash">$ name of virtual environmnet/bin/activate
</code></pre>
<p>For Windows Environment:</p>
<pre><code class="lang-bash">name of virtual environment\Scripts\activate
</code></pre>
<p>Since I am using a Windows machine, when I activate the environment it will look like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/FnmOzwRngsHOTcuJr4gMBnvyB6VhYGhcdyI2" alt="Image" width="97" height="21" loading="lazy"></p>
<p><em>You should see this at beginning of your command prompt line</em></p>
<p>The next step is to install flask on your virtual environment so that we can run the application inside our environment. Run the command:</p>
<pre><code class="lang-bash">pip install flask
</code></pre>
<p>Run your application and go to <a target="_blank" href="http://localhost:5000/">http://localhost:5000/</a></p>
<p>We finally made our web application. Now we want to show the whole world our project.</p>
<p>(More information on virtualenv can be found in the following guides on <a target="_blank" href="https://realpython.com/python-virtual-environments-a-primer/">virtualenv</a> and <a target="_blank" href="http://flask.pocoo.org/docs/0.12/installation/#installation">Flask Official Documentation</a>)</p>
<h2 id="heading-lets-send-it-to-the-cloud">Let’s send it to the Cloud</h2>
<p>To show others the project we made, we will need to learn how to use Cloud Services.</p>
<h3 id="heading-deploy-your-web-application-to-the-cloud">Deploy Your Web Application to the Cloud</h3>
<p>To deploy our web application to the cloud, we will use <a target="_blank" href="https://cloud.google.com/appengine/">Google App Engine</a> (Standard Environment). This is an example of a <strong>Platform as a Service (PaaS).</strong></p>
<p><strong>PaaS</strong> refers to the <strong>delivery of operating systems and associated services over the internet without downloads or installation</strong>. The approach lets customers create and deploy applications without having to invest in the underlying infrastructure (More info on PaaS check out <a target="_blank" href="https://searchitchannel.techtarget.com/definition/cloud-services">TechTarget</a>).</p>
<blockquote>
<p>Google App Engine is a platform as a service offering that allows developers and businesses to build and run applications using Google’s advanced infrastructure — <a target="_blank" href="https://www.techopedia.com/definition/31267/google-app-engine-gae">TechOpedia</a>.</p>
</blockquote>
<h4 id="heading-before-you-start"><strong>Before you Start:</strong></h4>
<p>You will need a <a target="_blank" href="https://accounts.google.com/signup/v2?hl=en&amp;flowName=GlifWebSignIn&amp;flowEntry=SignUp">Google Account</a><strong>.</strong> Once you create an account, go to the <a target="_blank" href="https://console.cloud.google.com">Google Cloud Platform Console</a> and create a new project. Also, you need to install the <a target="_blank" href="https://cloud.google.com/sdk/">Google Cloud SDK</a>.</p>
<p>At the end of this tutorial your project structure will look like this.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/Nb7fiERQFkKC5chGteCvWkQCkShVq7a1auUe" alt="Image" width="772" height="507" loading="lazy"></p>
<p><em>Project Folder Structure</em></p>
<p>We will need to create three new files: app.yaml, appengine_config.py, and requirements.txt.</p>
<p>Content of app.yaml:</p>
<pre><code class="lang-yml"><span class="hljs-attr">runtime:</span> <span class="hljs-string">python27</span>
<span class="hljs-attr">api_version:</span> <span class="hljs-number">1</span>
<span class="hljs-attr">threadsafe:</span> <span class="hljs-literal">true</span>

<span class="hljs-attr">handlers:</span>
<span class="hljs-bullet">-</span> <span class="hljs-attr">url:</span> <span class="hljs-string">/static</span>
  <span class="hljs-attr">static_dir:</span> <span class="hljs-string">static</span>
<span class="hljs-bullet">-</span> <span class="hljs-attr">url:</span> <span class="hljs-string">/.*</span>
  <span class="hljs-attr">script:</span> <span class="hljs-string">main.app</span>

<span class="hljs-attr">libraries:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">ssl</span>
    <span class="hljs-attr">version:</span> <span class="hljs-string">latest</span>
</code></pre>
<p>If you were to check <a target="_blank" href="https://cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env">Google’s Tutorial</a> in the part where they <strong>talk about content of the app.yaml</strong>, it does not include the section where I wrote about libraries.</p>
<p>When I first attempted to deploy my simple web app, my deployment never worked. After many attempts, I learned that we needed to include the SSL library.</p>
<p>The SSL Library allows us to <a target="_blank" href="https://wiki.python.org/moin/SSL">create secure connections between the client and server</a>. Every time the user goes to our website they will need to connect to a server run by Google App Engine. We need to create a secure connection for this. (I recently learned this, so if you have a suggestions for this let me know!)</p>
<p>Content of appengine_config.py:</p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> google.appengine.ext <span class="hljs-keyword">import</span> vendor

<span class="hljs-comment"># Add any libraries installed in the "lib" folder.</span>
vendor.add(<span class="hljs-string">'lib'</span>)
</code></pre>
<p>Content of requirements.txt:</p>
<pre><code class="lang-javascript">Flask
Werkzeug
</code></pre>
<p>Now inside our virtual environment <strong>(make sure your virtualenv is activated)</strong>, we are going to install the new dependencies we have in requirements.txt. Run this command:</p>
<pre><code class="lang-bash">pip install -t lib -r requirements.txt
</code></pre>
<p><strong>-t lib:</strong> This flag copies the libraries into a lib folder, which uploads to App Engine during deployment.</p>
<p><strong>-r requirements.txt:</strong> Tells pip to install everything from requirements.txt.</p>
<h3 id="heading-deploying-the-application">Deploying the Application</h3>
<p>To deploy the application to Google App Engine, use this command.</p>
<pre><code class="lang-bash">gcloud app deploy
</code></pre>
<p>I usually include — <strong>project [ID of Project]</strong></p>
<p>This specifies what project you are deploying. The command will look like this:</p>
<pre><code class="lang-bash">gcloud app deploy --project [ID of Project]
</code></pre>
<h3 id="heading-the-application">The Application</h3>
<p>Now check the URL of your application. The application will be store in the following way:</p>
<pre><code class="lang-bash"><span class="hljs-string">"your project id"</span>.appspot.com
</code></pre>
<p>My application is here: <a target="_blank" href="http://sal-flask-tutorial.appspot.com">http://sal-flask-tutorial.appspot.com</a></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>From this tutorial, you all learned how to:</p>
<ul>
<li><p>Use the framework called Flask to use Python as a Server Side Language.</p>
</li>
<li><p>Learned how to use HTML, CSS, and Flask to make a website.</p>
</li>
<li><p>Learned how to create Virtual Environments using virtualenv.</p>
</li>
<li><p>Use Google App Engine Standard Environment to deploy an application to the cloud.</p>
</li>
</ul>
<h4 id="heading-what-i-learned"><strong>What I learned</strong></h4>
<p>I learned three important things from this small project.</p>
<p><strong>First, I learned about the difference between a static website and a web application</strong></p>
<p><strong>Static Websites:</strong></p>
<ul>
<li>Means that the server is serving HTML, CSS, and JavaScript files to the client. The content of the site does not change when the user interacts with it.</li>
</ul>
<p><strong>Web Applications:</strong></p>
<ul>
<li>A web application or dynamic website generates content based on retrieved data (most of the time is a database) that changes based on a user’s interaction with the site. In a web application, the server is responsible for querying, retrieving, and updating data. This causes web applications to be slower and more difficult to deploy than static websites for simple applications (<a target="_blank" href="https://www.reddit.com/r/Python/comments/1iewqt/deploying_static_flask_sites_for_free_on_github/">Reddit</a>).</li>
</ul>
<p><strong>Server Side and Client Side:</strong></p>
<ul>
<li>I learned that a web application has two sides. The client side and the server side. The client side is what the user interacts with and the server side is where the all the information that the user inputted is processed.</li>
</ul>
<p><strong>Second, I learned about Cloud Services</strong></p>
<p>Most of my previous projects were static websites, and to deploy them I used <a target="_blank" href="https://pages.github.com/">GitHub Pages</a>. GitHub Pages is a free static site hosting service designed to host projects from a GitHub Repository.</p>
<p>When working with web applications, I could not use GitHub Pages to host them. GitHub Pages is only meant for static websites not for something dynamic like a web application that requires a server and a database. I had to use Cloud Services such as <a target="_blank" href="https://aws.amazon.com/">Amazon Web Services</a> or <a target="_blank" href="https://www.heroku.com/">Heroku</a></p>
<p><strong>Third, I learned how to use Python as a Server Side Language</strong></p>
<p>To create the server side of the web application we had to use a server side language. I learned that I could use the framework called Flask to use Python as the Server Side Language.</p>
<h4 id="heading-next-steps"><strong>Next Steps:</strong></h4>
<p>You can build all sorts of things with Flask. I realized that Flask helps make the code behind the website easier to read. I have made the following applications during this summer of 2018 and I hope to make more.</p>
<p>Personal Projects</p>
<ul>
<li><p><a target="_blank" href="http://twilio-pokedex.appspot.com/">A Twilio SMS App</a></p>
</li>
<li><p><a target="_blank" href="http://salvador-villalon.appspot.com/">My Personal Website</a></p>
</li>
</ul>
<p>During my internship</p>
<ul>
<li><a target="_blank" href="http://spgi2018-container-project.appspot.com/">Part of a project where I learned about Docker and Containers</a></li>
</ul>
<p>Here is the list of resources that helped me create this tutorial:</p>
<ul>
<li><p>“App Engine — Build Scalable Web &amp; Mobile Backends in Any Language | App Engine | Google Cloud.” <em>Google</em>, Google, <a target="_blank" href="https://cloud.google.com/appengine/">cloud.google.com/appengine/</a>.</p>
</li>
<li><p>“Building a Website with Python Flask.” <em>PythonHow</em>, <a target="_blank" href="https://pythonhow.com/building-a-website-with-python-flask/">pythonhow.com/building-a-website-with-python-flask/</a>.</p>
</li>
<li><p>“Flask — Lecture 2 — CS50’s Web Programming with Python and JavaScript.” <em>YouTube</em>, 6 Feb. 2018, <a target="_blank" href="https://youtu.be/j5wysXqaIV8">youtu.be/j5wysXqaIV8</a>.</p>
</li>
<li><p>“Getting Started with Flask on App Engine Standard Environment | App Engine Standard Environment for Python | Google Cloud.” <em>Google</em>, Google, <a target="_blank" href="https://cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env">cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env</a>.</p>
</li>
<li><p>“Installation.” <em>Welcome | Flask (A Python Microframework)</em>, <a target="_blank" href="http://flask.pocoo.org/docs/0.12/installation/">flask.pocoo.org/docs/0.12/installation/</a>.</p>
</li>
<li><p>“Python — Deploying Static Flask Sites for Free on Github Pages.” <em>Reddit</em>, <a target="_blank" href="http://www.reddit.com/r/Python/comments/1iewqt/deploying_static_flask_sites_for_free_on_github/.">www.reddit.com/r/Python/comments/1iewqt/deploying_static_flask_sites_for_free_on_github/.</a></p>
</li>
<li><p>Real Python. “Python Virtual Environments: A Primer — Real Python.” <em>Real Python</em>, Real Python, 7 Aug. 2018, <a target="_blank" href="https://realpython.com/python-virtual-environments-a-primer/">realpython.com/python-virtual-environments-a-primer/</a>.</p>
</li>
<li><p>“What Is Cloud Services? — Definition from WhatIs.com.” <em>SearchITChannel</em>, <a target="_blank" href="https://searchitchannel.techtarget.com/definition/cloud-services">searchitchannel.techtarget.com/definition/cloud-services</a>.</p>
</li>
<li><p>“What Is Google App Engine (GAE)? — Definition from Techopedia.” <em>Techopedia.com</em>, <a target="_blank" href="http://www.techopedia.com/definition/31267/google-app-engine-gae.">www.techopedia.com/definition/31267/google-app-engine-gae.</a></p>
</li>
</ul>
<p>If you have any suggestions or questions, feel free to leave a comment.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
