{"id":3327,"date":"2024-08-16T14:12:08","date_gmt":"2024-08-16T17:12:08","guid":{"rendered":"https:\/\/horatech.shop\/?p=3327"},"modified":"2024-11-29T14:15:19","modified_gmt":"2024-11-29T17:15:19","slug":"como-fazer-um-crud-simples-com-python-e-flask","status":"publish","type":"post","link":"https:\/\/horatech.shop\/en\/como-fazer-um-crud-simples-com-python-e-flask\/","title":{"rendered":"How to make a simple CRUD with Python and Flask"},"content":{"rendered":"<p>In this article, you will learn to create a <strong>Crud (Create, Read, Update, Delete)<\/strong> Simple using python and flask. <\/p>\n\n\n\n<p>Flask, being a python micro-framework, offers a simple and flexible way to implement these features, ideal for both those starting and for more experienced developers.<\/p>\n\n\n\n<p>Let's explore the main concepts and practices together to develop a Python and Flask Crud:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What is Crud and why it is important in web development.<\/li>\n\n\n\n<li>Introduction to Flask, a Python micro-framework.<\/li>\n\n\n\n<li>How to configure your development environment.<\/li>\n\n\n\n<li>CRUD project structure using Python and Flask.<\/li>\n\n\n\n<li>How to connect to the database to store the data.<\/li>\n\n\n\n<li>Tests to ensure that CRUD works properly.<\/li>\n\n\n\n<li>Tips for optimizing your application and code.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"854\" height=\"470\" src=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-um-CRUD-simples-com-Python-e-Flask.webp?resize=854%2C470&#038;ssl=1\" alt=\"\" class=\"wp-image-3328\" srcset=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-um-CRUD-simples-com-Python-e-Flask.webp?w=854&amp;ssl=1 854w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-um-CRUD-simples-com-Python-e-Flask.webp?resize=300%2C165&amp;ssl=1 300w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-um-CRUD-simples-com-Python-e-Flask.webp?resize=768%2C423&amp;ssl=1 768w\" sizes=\"(max-width: 854px) 100vw, 854px\" \/><figcaption class=\"wp-element-caption\"><em>Creating a simple python raw<\/em><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Flask Introduction<\/h2>\n\n\n\n<p>Flask is a python-developed micro-framework designed to make it easier to create web applications. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"851\" height=\"466\" src=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/introducao-ao-flask.webp?resize=851%2C466&#038;ssl=1\" alt=\"\" class=\"wp-image-3329\" srcset=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/introducao-ao-flask.webp?w=851&amp;ssl=1 851w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/introducao-ao-flask.webp?resize=300%2C164&amp;ssl=1 300w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/introducao-ao-flask.webp?resize=768%2C421&amp;ssl=1 768w\" sizes=\"(max-width: 851px) 100vw, 851px\" \/><figcaption class=\"wp-element-caption\">Flask is easy to use and makes the&nbsp;<em>Web development with python<\/em>&nbsp;more affordable.<\/figcaption><\/figure>\n\n\n\n<p>This makes it an excellent choice for both small applications and more complex projects.<\/p>\n\n\n\n<p>The Flask community is active, and documentation is complete, which facilitates learning and development. <\/p>\n\n\n\n<p>In addition, there are many extensions available, which make development even faster and more efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a crud?<\/h2>\n\n\n\n<p>Crud is a acronym that represents the four basic operations for data manipulation in one application: <strong>Create (Create)<\/strong>, <strong>Read (read)<\/strong>, <strong>Update (update)<\/strong> e <strong>Delete (Delete)<\/strong>.<\/p>\n\n\n\n<p>Here is a summary of CRUD operations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Create<\/strong>: Adds new records to the database.<\/li>\n\n\n\n<li><strong>Read<\/strong>: Displays the information already stored.<\/li>\n\n\n\n<li><strong>Update<\/strong>: Modifies existing data.<\/li>\n\n\n\n<li><strong>Delete<\/strong>: Removes records that are no longer needed.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"720\" height=\"433\" src=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/O-que-e-um-CRUD.webp?resize=720%2C433&#038;ssl=1\" alt=\"\" class=\"wp-image-3330\" srcset=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/O-que-e-um-CRUD.webp?w=720&amp;ssl=1 720w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/O-que-e-um-CRUD.webp?resize=300%2C180&amp;ssl=1 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><em>Understanding Crud<\/em><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Why use python to create a crud?<\/h2>\n\n\n\n<p>Python is a programming language famous for its simplicity and readability, which facilitates the creation of a CRUD. <\/p>\n\n\n\n<p>It allows developers to focus on business logic without worrying about complicated syntax.<\/p>\n\n\n\n<p>With Python, you also have access to a wide variety of development libraries, such as Flask and SQLALCHEMY, which facilitate integration with databases and user authentication. <\/p>\n\n\n\n<p>The strong Python community supports continuous support through tutorials, forums and documentation, which helps both beginners and experienced developers to overcome challenges quickly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing the development environment<\/h2>\n\n\n\n<p>Before you start developing a Python and Flask Crud, it is important to prepare your development environment. <\/p>\n\n\n\n<p>First, install Python on your system, which can be done directly from the official Python website. <\/p>\n\n\n\n<p>The installation of a package manager like the <code>pip<\/code> It is also essential to add the libraries necessary to your project.<\/p>\n\n\n\n<p>Choosing a good code editor, such as Visual Studio Code or Pycharm, is critical to facilitating development. <\/p>\n\n\n\n<p>These editors offer advanced features such as autocompleting and plugins support, as well as an integrated terminal to execute commands and interact with their application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Flask Installation<\/h2>\n\n\n\n<p>Installing Flask is the first step in starting to develop with this micro-framework. <code>pip<\/code>, the command to install Flask is simple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PIP INSTALL FLASK<\/code><\/pre>\n\n\n\n<p>It is a good practice to activate a virtual environment before installing Flask.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Structure<\/h2>\n\n\n\n<p>A good design structure facilitates the development and maintenance of the application.<\/p>\n\n\n\n<p>Here is a basic structure for a Flask project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>APP\/<\/strong>: Contains the main file of the application, as <code>app.py<\/code>.<\/li>\n\n\n\n<li><strong>Templates\/<\/strong>: Stores the HTML files to render the pages.<\/li>\n\n\n\n<li><strong>STATIC\/<\/strong>: Contains static files such as CSS, JavaScript and images.<\/li>\n\n\n\n<li><strong>Config\/<\/strong>: Stores environment settings and project constants.<\/li>\n\n\n\n<li><strong>VENV\/<\/strong>: Virtual environment to manage project dependencies.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Making a Crud with Python<\/h2>\n\n\n\n<p>With Flask, creating a CRUD involves defining routes that connect URLs to the functions that perform each operation. <\/p>\n\n\n\n<p>For each of the CRUD operations, as <code>\/Create<\/code>, <code>\/Read<\/code>, <code>\/update<\/code> e <code>\/Delete<\/code>, we use the decorator <code>@app.rout ()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Routes Implementation<\/h2>\n\n\n\n<p>The organization of routes is essential for the functioning of CRUD.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operation<\/th><th>Route<\/th><th>Http method<\/th><\/tr><\/thead><tbody><tr><td>To create<\/td><td>\/Create<\/td><td>Post<\/td><\/tr><tr><td>To read<\/td><td>\/Read<\/td><td>Get<\/td><\/tr><tr><td>To update<\/td><td>\/update<\/td><td>Put<\/td><\/tr><tr><td>Delete<\/td><td>\/Delete<\/td><td>Delete<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the user interface<\/h2>\n\n\n\n<p>A friendly user interface is crucial to the success of CRUD. <\/p>\n\n\n\n<p>Elements such as forms, buttons and tables are important to ensure a good experience to the user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting to the database<\/h2>\n\n\n\n<p>For CRUD to work properly, it is essential to connect Flask to a database. <\/p>\n\n\n\n<p>Using SQLALCHEMY, an Object-Relational Mapping (ORM) library, facilitates this integration further, allowing you to interact with the database using simple python commands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the created Crud<\/h2>\n\n\n\n<p>Testing CRUD is essential to ensure that all features are working as expected.<\/p>\n\n\n\n<p>Debugging tools and automated tests, such as <code>pytest<\/code>, are very useful for validating the code and identifying errors quickly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Optimizing code and application<\/h2>\n\n\n\n<p>After creating CRUD, it is important to optimize both code and application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Building a simple Python and Flask Crud is an excellent exercise to learn the foundations of web development. <\/p>\n\n\n\n<p>This project helps you understand important concepts such as route creation, database connection and user interface development. <\/p>\n\n\n\n<p>It is an important step for those starting and want to delve into the web development world with Python.<\/p>\n\n\n\n<p>Creating a raw is just the beginning.<\/p>","protected":false},"excerpt":{"rendered":"<p>Neste artigo, voc\u00ea vai aprender a criar um CRUD (Create, Read, Update, Delete) simples usando Python e Flask. Um CRUD \u00e9 fundamental em qualquer aplicativo web, pois permite manipular dados de maneira f\u00e1cil e organizada. O Flask, sendo um micro-framework em Python, oferece uma forma simples e flex\u00edvel de implementar essas funcionalidades, ideal tanto para [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3328,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3327","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programacao"],"blocksy_meta":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-um-CRUD-simples-com-Python-e-Flask.webp?fit=854%2C470&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts\/3327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/comments?post=3327"}],"version-history":[{"count":3,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts\/3327\/revisions"}],"predecessor-version":[{"id":4303,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts\/3327\/revisions\/4303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/media\/3328"}],"wp:attachment":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/media?parent=3327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/categories?post=3327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/tags?post=3327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}