Use Python to develop a [personal plan todolist]

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


There is a saying that goes like this: "Everything is done in advance, and if it is not in advance, it will be abolished." It means that before we do something, it is best to make your plan, and then implement it in an orderly manner, so as not to generate too much Mistakes and unhappiness, if you don't prepare for anything, people will easily be abandoned.

Did you scrap today?

 

In order to prevent us from scrapping so quickly, how about we use Python to write a web version of todolist and then make our own plans in it?

 

 

You may think of simple web interactions like this, which can be achieved with Flask, but today I will introduce you to another lightweight web framework, it is called bottle, and relatively few people know it, but it is also It's easy to use.

If we want to write the plan content in the website, we need to use a database, we can also use a lightweight sqlite database to achieve, this Python has its own sqlite3 library.

You can use it to create a todo data table:

 

The task field is mainly used to store the content of the plan to be added by the user, and status is used to mark the status of whether the user has completed the plan. For example, 1 means incomplete, 0 means completed.

You can insert the data of each task through insert:

 

 

carried out:

 

At this time, a database file will be generated in your current directory, and all plan data will be stored in it later:

 

If you haven't installed bottle before, you can use pip to install a wave. It does not depend on other libraries, and the installation will be done quickly.

Import bottle after installation:

 

 

Define the route, this method will be called when you visit the /todo homepage:

 

At this time, you can connect to the database we just created, find out the unfinished tasks by the way, and then return the results for display:

 

 

Then execute the service, you can specify which port to run on the server, here I specify the port 8888:

 


After running Python, you can open it in your browser: localhost:8888/todo

 

As you can see, the data we just inserted is displayed, indicating that the service and database are operating normally.

Then the data will be displayed on the home page, and the bottle's template engine can be used to bind the data and create a make_table.tpl file in the current directory.

 

Here we use the HTML template markup language, we can write the results obtained in the database in the rows in the Python file just now, and then use it in the make_table template file to loop through the relevant data:

 

Run a wave:

 

 

The data is successfully bound.

Next, insert the content of the plan. You can use bottle's request to make Get and Post requests.

Create a new_task.tpl template file:

 

Here defines a form form, let it request the new method, you can define this method in py, let’s show it first:

 

 

Run a wave:

 


Then when the user clicks submit, the content of the input box needs to be obtained and then saved in the database, which can be defined in the new method:

Here is mainly to obtain the data of the input box through GET.task, and then insert the data into the database. After the successful addition, a reminder of successful addition will be displayed.

Run it:

 

 

Yes, you can go back to the homepage and you can see that the data has indeed been added:

 

You can add a plan. The next thing to achieve is to edit the plan, allowing users to choose whether each plan is completed, and to modify the content of the plan:

 

You can write an edit_task.tpl template file to display and submit data:

 

The request bound to the form here is the edit method, where { {no}} refers to the id in the plan table, so that specific plan data can be modified according to the id.

If you want to display the relevant plan content and status in the input tag, you can get it from the database in py and return it, and put it in the old parameter, like this:

When the user submits the editing operation, the content and status of the plan can be obtained, and then the plan data in the database can be modified according to the id:

 

The <no:int> in the route definition means that we want to receive an int type data with no parameter, which is id.

Run a wave like this:

 

 

Back to the homepage, you can see that the completed plan disappears:

 


In this way, the main functions required by a personal plan are realized. Of course, the page is a bit ugly. You can decorate it with CSS. This is for you to play with. This time I mainly want you to understand the use of bottle. Wait for me. Send you the source code, you can make more improvements based on this.

For the content of each task, you can also write a method to query, and even you can use bottle to write an API to return Json directly:

 

At this time, you can access the interface like this:

 

 

Of course, you can quickly customize the 404 page, using bottle's error module to achieve:

 

 


ok, above.

ps: This tutorial mainly refers to the tutorials of bottle official documents, and attaches a link to the official documents of bottle:

http://bottlepy.org/docs/dev/index.html

I hope that this way of presentation will make it easier for you to understand the use of the lightweight web framework bottle. In addition to Flask, this is also a good choice. Writing some web pages is still a leverage. I hope it will help you~

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112935872