{"id":2239,"date":"2024-09-10T15:00:55","date_gmt":"2024-09-10T15:00:55","guid":{"rendered":"http:\/\/suimy.me\/?p=2239"},"modified":"2024-09-11T17:08:41","modified_gmt":"2024-09-11T17:08:41","slug":"getting-started-with-flask","status":"publish","type":"post","link":"http:\/\/suimy.me\/index.php\/2024\/09\/10\/getting-started-with-flask\/","title":{"rendered":"Getting Started with Flask"},"content":{"rendered":"

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

\"Flask\"Flask<\/span><\/span><\/figure>\n

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 Slim<\/a>. If you\u2019re coming from Ruby<\/a>, you might find Flask similar to Sinatra<\/a>.<\/p>\n

Let\u2019s see how we can get started with Flask<\/strong> and build a simple web page.<\/p>\n

Install Flask<\/h4>\n

First, we need to install Flask. We can do this by running:<\/p>\n

\r\npip install flask\r\n<\/pre>\n

Create the App File<\/h4>\n

Next, we need to create a Python file, for example, app.py<\/code>. To get started, we add the following code:<\/p>\n

\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

The Flask<\/code> class is used to create an instance of the app. The @app.route('\/')<\/code> decorator maps the homepage URL, \/<\/code>, to the home<\/code> function, which returns the message \u201cHello Flask!\u201d<\/strong>.<\/p>\n

Run the App<\/h4>\n

Now, run the app using the following command:<\/strong><\/p>\n

\r\nflask run --debug\r\n<\/pre>\n

When we visit http:\/\/127.0.0.1:5000\/<\/code> in the web browser, we\u2019ll see the message \u201cHello Flask!\u201d<\/strong>.<\/p>\n

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

Routing<\/h4>\n

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 @app.route('\/')<\/code> decorator. This tells Flask to call the home<\/code> function when the user visits the homepage URL, \/<\/code>.<\/p>\n

We can define routes for other pages as well. For example, to create an About<\/code> page, we can add the following code:<\/p>\n

\r\n@app.route('\/about')\r\ndef about():\r\n    return \"This is the About page.\"\r\n<\/pre>\n

Now, if we load http:\/\/127.0.0.1:5000\/about<\/code>, we\u2019ll see the message \u201cThis is the About page.\u201d<\/strong>.<\/p>\n

Wrapping up<\/h4>\n

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

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

So stay tuned!<\/p>\n

The post Getting Started with Flask<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

Flask is a lightweight and flexible micro-framework for Python that makes it easy to get started with web development. It\u2019s […]<\/p>\n","protected":false},"author":1,"featured_media":2241,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/posts\/2239"}],"collection":[{"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/comments?post=2239"}],"version-history":[{"count":2,"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/posts\/2239\/revisions"}],"predecessor-version":[{"id":2242,"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/posts\/2239\/revisions\/2242"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/media\/2241"}],"wp:attachment":[{"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/media?parent=2239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/categories?post=2239"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/suimy.me\/index.php\/wp-json\/wp\/v2\/tags?post=2239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}