{"id":72853,"date":"2024-09-10T23:00:55","date_gmt":"2024-09-10T15:00:55","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=72853"},"modified":"2024-10-29T19:55:28","modified_gmt":"2024-10-29T11:55:28","slug":"getting-started-with-flask","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/","title":{"rendered":"Getting Started with Flask"},"content":{"rendered":"<p><strong>Flask<\/strong> is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It\u2019s designed to be simple and minimalistic, offering essential tools and features needed for building a web application, while allowing developers to have full control over how to implement additional features.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/getting-started-with-flask\/flask.jpg\" alt=\"Flask logo\" width=\"750\" height=\"480\"><\/figure>\n<p>It is a \u201cmicro-framework,\u201d which means it doesn\u2019t require specific tools or libraries. This gives developers the freedom to decide how they want to extend their application, making Flask a good choice for those who prefer flexibility and customization. If you\u2019re coming from PHP, this could be an alternative to using other micro-frameworks like <a rel=\"noopener\" target=\"_blank\" href=\"https:\/\/www.slimframework.com\/\">Slim<\/a>. If you\u2019re coming from <a rel=\"noopener\" target=\"_blank\" href=\"https:\/\/www.ruby-lang.org\/en\/\">Ruby<\/a>, you might find Flask similar to <a rel=\"noopener\" target=\"_blank\" href=\"https:\/\/sinatrarb.com\/\">Sinatra<\/a>.<\/p>\n<p>Let\u2019s see how we can get started with <strong>Flask<\/strong> and build a simple web page.<\/p>\n<h2>Install Flask<\/h2>\n<p>First, we need to install Flask. We can do this by running:<\/p>\n<pre>\r\npip install flask\r\n<\/pre>\n<h2>Create the App File<\/h2>\n<p>Next, we need to create a Python file, for example, <code>app.py<\/code>. To get started, we add the following code:<\/p>\n<pre>\r\nfrom flask import Flask\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef home():\r\n    return \"Hello Flask!\"\r\n<\/pre>\n<p>The <code>Flask<\/code> class is used to create an instance of the app. The <code>@app.route('\/')<\/code> decorator maps the homepage URL, <code>\/<\/code>, to the <code>home<\/code> function, which returns the message <strong>\u201cHello Flask!\u201d<\/strong>.<\/p>\n<h2>Run the App<\/h2>\n<p><strong>Now, run the app using the following command:<\/strong><\/p>\n<pre>\r\nflask run --debug\r\n<\/pre>\n<p>When we visit <code>http:\/\/127.0.0.1:5000\/<\/code> in the web browser, we\u2019ll see the message <strong>\u201cHello Flask!\u201d<\/strong>.<\/p>\n<p>In this case, we also run the app in debug mode, which automatically reloads the server when you make changes to the code. This is useful for development, as you can see the changes immediately without having to restart the server. Keep in mind that you\u2019d want to disable debug mode when deploying your application to a production server.<\/p>\n<h2>Routing<\/h2>\n<p>One of the main features provided by Flask is routing. Routing is the process of mapping URLs to functions that handle requests. In the example above, we defined a route for the homepage using the <code>@app.route('\/')<\/code> decorator. This tells Flask to call the <code>home<\/code> function when the user visits the homepage URL, <code>\/<\/code>.<\/p>\n<p>We can define routes for other pages as well. For example, to create an <code>About<\/code> page, we can add the following code:<\/p>\n<pre>\r\n@app.route('\/about')\r\ndef about():\r\n    return \"This is the About page.\"\r\n<\/pre>\n<p>Now, if we load <code>http:\/\/127.0.0.1:5000\/about<\/code>, we\u2019ll see the message <strong>\u201cThis is the About page.\u201d<\/strong>.<\/p>\n<h2>Wrapping up<\/h2>\n<p>Flask is a great choice for both beginners and experienced developers, especially when you want flexibility without the complexity of larger frameworks like Django.<\/p>\n<p>In this article, we\u2019ve covered the basics of how Flask works with some simple examples such as how to spin up the development server, and we learned a bit about routing in Flask to create static pages. In future articles, we\u2019ll explore more advanced topics like rendering templates, working with forms, handling requests, and connecting to databases.<\/p>\n<p>So stay tuned!<\/p>","protected":false},"excerpt":{"rendered":"<p>Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It\u2019s designed to be simple and minimalistic, offering essential tools and features needed for building a web application, while allowing developers to have full control over how to implement additional features. It is a \u201cmicro-framework,\u201d which&hellip;<\/p>\n","protected":false},"author":113,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[],"topic":[],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Getting Started with Flask - Hongkiat<\/title>\n<meta name=\"description\" content=\"Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It&#039;s designed to be simple and\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting Started with Flask\" \/>\n<meta property=\"og:description\" content=\"Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It&#039;s designed to be simple and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-10T15:00:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-29T11:55:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/getting-started-with-flask\/flask.jpg\" \/>\n<meta name=\"author\" content=\"Thoriq Firdaus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@tfirdaus\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thoriq Firdaus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/\"},\"author\":{\"name\":\"Thoriq Firdaus\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e7948c7a175d211496331e4b6ce55807\"},\"headline\":\"Getting Started with Flask\",\"datePublished\":\"2024-09-10T15:00:55+00:00\",\"dateModified\":\"2024-10-29T11:55:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/\"},\"wordCount\":452,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/getting-started-with-flask\\\/flask.jpg\",\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/\",\"name\":\"Getting Started with Flask - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/getting-started-with-flask\\\/flask.jpg\",\"datePublished\":\"2024-09-10T15:00:55+00:00\",\"dateModified\":\"2024-10-29T11:55:28+00:00\",\"description\":\"Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It's designed to be simple and\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/getting-started-with-flask\\\/flask.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/getting-started-with-flask\\\/flask.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/getting-started-with-flask\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Getting Started with Flask\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e7948c7a175d211496331e4b6ce55807\",\"name\":\"Thoriq Firdaus\",\"description\":\"Thoriq is a writer for Hongkiat.com with a passion for web design and development. He is the author of Responsive Web Design by Examples, where he covered his best approaches in developing responsive websites quickly with a framework.\",\"sameAs\":[\"https:\\\/\\\/thoriq.com\",\"https:\\\/\\\/x.com\\\/tfirdaus\"],\"jobTitle\":\"Web Developer\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/thoriq\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Getting Started with Flask - Hongkiat","description":"Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It's designed to be simple and","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/","og_locale":"en_US","og_type":"article","og_title":"Getting Started with Flask","og_description":"Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It's designed to be simple and","og_url":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2024-09-10T15:00:55+00:00","article_modified_time":"2024-10-29T11:55:28+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/getting-started-with-flask\/flask.jpg","type":"","width":"","height":""}],"author":"Thoriq Firdaus","twitter_card":"summary_large_image","twitter_creator":"@tfirdaus","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Thoriq Firdaus","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/"},"author":{"name":"Thoriq Firdaus","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e7948c7a175d211496331e4b6ce55807"},"headline":"Getting Started with Flask","datePublished":"2024-09-10T15:00:55+00:00","dateModified":"2024-10-29T11:55:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/"},"wordCount":452,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/getting-started-with-flask\/flask.jpg","articleSection":["Coding"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/","url":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/","name":"Getting Started with Flask - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/getting-started-with-flask\/flask.jpg","datePublished":"2024-09-10T15:00:55+00:00","dateModified":"2024-10-29T11:55:28+00:00","description":"Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It's designed to be simple and","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/getting-started-with-flask\/flask.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/getting-started-with-flask\/flask.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/getting-started-with-flask\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Getting Started with Flask"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e7948c7a175d211496331e4b6ce55807","name":"Thoriq Firdaus","description":"Thoriq is a writer for Hongkiat.com with a passion for web design and development. He is the author of Responsive Web Design by Examples, where he covered his best approaches in developing responsive websites quickly with a framework.","sameAs":["https:\/\/thoriq.com","https:\/\/x.com\/tfirdaus"],"jobTitle":"Web Developer","url":"https:\/\/www.hongkiat.com\/blog\/author\/thoriq\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-iX3","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/72853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/113"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=72853"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/72853\/revisions"}],"predecessor-version":[{"id":73012,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/72853\/revisions\/73012"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=72853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=72853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=72853"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=72853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}