Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • F FastAPI
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • Sitemule
  • Public
  • workshops
  • open source on IBM i
  • FastAPI
  • Wiki
  • Part 2b. Organise my first service

Part 2b. Organise my first service · Changes

Page history
Update Part 2b. Organise my first service authored Oct 30, 2021 by Jens Melgard Churchill's avatar Jens Melgard Churchill
Hide whitespace changes
Inline Side-by-side
Part-2b.-Organise-my-first-service.md
View page @ 7419926a
......@@ -64,6 +64,37 @@ But no one wants to inline HTML into code, and since we just installed a templat
Now refactoring code using an unfamiliar editor is a daunting task, so depending on how long it takes, we might have to skip ahead after this. Keep calm and don't panic, it'll be fine.
New vim commands which might be useful.
In NORMAL-mode (not insert).
* `gg` goes to top of file
* `GG` goes to bottom
* `/` searches a file, using regex. (<Enter> to search, `n` to next, `:noh` to clear search results)
* `yy` on a line, copies it
* `dd` on a line, cuts it
* `p` pastes below current line
A lot of vim commands can be composed, so typing `10G` will move the cursor to line 10 in the file.
`d5d` will cut from current line, and 5 down, there are more, a lot more.
But this isn't actually workshop on vim, but remember this...
The designers of vim believe that people _edit_ files more than they _create_ them,
so vim is optimised towards modifying existing files, as appose to creating them.
Pressing `v` puts vim into VISUAL-mode, as appose to `i` for INSERT-mode.
In _visual-mode_ you can use the cursors to select in the buffer.
As an example, if we are on line 10, press `v` and pres the down key a couple of times,
and then press `d` - We'll cut those lines.
And that brings us to the most import key, the one to rule them all.
`u` will undo ;)
Last one before we start, in _visual-mode_ select a few lines, and press `>`
you just indented right.
Lets take a look at the final result for _main.py_
```python
......@@ -114,9 +145,58 @@ We've introduced to new features, namely support for _templates_ and _static fil
The index function has gotten a new annotation, that "links" it to a template file.
And we've removed the items route.
As always, press `Esc` to ext insert-mode, and :w to save.
As always, press `Esc` to exit insert-mode, and :w to save.
Now come the hard part, the need to work with multiple files.
The easy way, is to simply exit vim, and start vim on a new file, that could be `:q` and `vim api_router.py`
But for the brave, working with multiple files or buffers as vim calls them is a powerful skill.
Here are the most useful commands...
- To open a file (buffer) type `:e {filepath}` you can use tab-completion.
- To list open buffers type `:ls`, note the number (n) to the left of each.
- To switch buffers type `:b<n>`
- To go to next buffer type `:bn`, and `:bp` for previous
- To delete a buffer (~close it), type `:bd`
(this does not delete the file)
It is also possible to go to an open buffer, by knowing its name, or part of it.
Typing `:b <filename>` jumps to the buffer with that file, and of course, tab-completion is your friend.
And finally, typing `:e .` will allow you to navigate the file system to open a file! ( _Now he tells me!_ )
So, lets try...
Navigate to the bottom of main.py, down to where the `@router.get("/items/{item_id}")` line is.
Press `v` and arrow down to until the whole code block is selected.
Press `d` to cut it, and `:w` to save the change.
Type `:e api+<tab>` or `:e .` and navigate to open the api_router.py file we created earlier.
Since this file is empty, simply press `p`to paste the cut-content from before.
Press `gg` or arrow back up to the top of the file, and type in the remaining bits.
Press `i` to enter _insert-mode_, and change the `@app` part to `@router`
When the file looks like this, the free coffee is on me.
```python
from fastapi import APIRouter
from typing import Optional
router = APIRouter()
@router.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}
```
`Esc`to return to _normal-mode_ and type `:w`to save, and delete that buffer `:bd`
You are back in main.py, well done!! - I'll take it from here!
Proceed to Part 3.
Now come the hard part, the need to changes files.
The easy way, is to simply exit vim, and start it on a new file, that is `:q` and `vim api_router.py`
But for the brave, working with multiple files, or buffers as vim calls them, is bordering on the advanced.
\ No newline at end of file
Clone repository
  • Part 1a. My first service
  • Part 1b. Testing my first service
  • Part 2a. Extending my first service
  • Part 2b. Organise my first service
  • Part 3a. Review my first service
  • Part 3b. Connecting my first service
  • Home