Skip to content

4. Snippets

  1. Trykk CMD+SHIFT+p / CTRL+SHIFT+p
  2. Skriv og velg Snippets: Configure Snippets
  3. Lim inn koden under.
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
"Lese txt-fil": {
"prefix": "lestxt",
"scope": "python",
"description": "Les txt-fil",
"body": [
"with open(\"$1\") as fil:",
" data = fil.read().split(\"\\n\")",
"$0"
],
},
"Les JSON": {
"prefix": "lesjson",
"scope": "python",
"description": "Les JSON-fil",
"body": [
"import json",
"with open(\"$1\", encoding=\"utf-8\") as fil:",
" data = json.load(fil)",
"$0"
]
},
"Les CSV": {
"prefix": "lescsv",
"scope": "python",
"description": "Les CSV-fil som en liste med ordbøker",
"body": [
"import csv",
"with open(\"$1\", encoding=\"utf-8\") as fil:"
" leser = csv.DictReader(fil)",
" data = list(leser)",
"$0"
]
},
"Flask oppsett": {
"prefix": "flaskoppsett",
"scope": "python",
"description": "Oppsett av Flask-app",
"body": [
"from flask import Flask, render_template, request",
"",
"app = Flask(__name__)",
"",
"$0",
"",
"if __name__ == \"__main__\":",
" app.run(debug=True)",
]
},
"Flask get-rute": {
"prefix": "flask-get",
"scope": "python",
"description": "En get-rute i Flask",
"body": [
"@app.get(\"/$1\")",
"def get_$2():",
" return render_template(\"$3.html\")",
"$0"
]
},
"Plotly CDN": {
"prefix": "plotlycdn",
"scope": "html",
"body": [
"<script src=\"https://cdn.plot.ly/plotly-3.4.0.min.js\" charset=\"utf-8\"></script>",
"$0"
],
"description": "Lenke til Plotly-cdn"
},
"Plotly stolpediagram": {
"prefix": "plotbar",
"scope": "html",
"body": [
"<!-- Husk å importere Plotly-biblioteket: 'plotlycdn' -->",
"<div id=\"divPlott\"></div>",
"<script>",
"Plotly.newPlot(\"divPlott\", [{",
" x: {{ ${1} | tojson }},",
" y: {{ ${2} | tojson }},",
" type: \"bar\"",
"}])",
"</script>",
"$0"
],
"description": "Plotly bar chart med Jinja2-variabler"
},
"Plotly kakediagram": {
"prefix": "plotpie",
"scope": "html",
"body": [
"<!-- Husk å importere Plotly-biblioteket: 'plotlycdn' -->",
"<div id=\"divPlott\"></div>",
"<script>",
"Plotly.newPlot(\"divPlott\", [{",
" labels: {{ ${1} | tojson }},",
" values: {{ ${2} | tojson }},",
" type: \"pie\"",
"}])",
"</script>",
"$0"
],
"description": "Plotly pie chart med Jinja2-variabler"
},
"Pygame Game Template ": {
"prefix": "pygameclass",
"body": [
"import pygame",
"",
"# Constants",
"FPS = 60",
"WIDTH = 800",
"HEIGHT = 600",
"",
"class Game:",
" def __init__(self) -> None:",
" pygame.init()",
" self.clock = pygame.time.Clock()",
" self.screen = pygame.display.set_mode((WIDTH, HEIGHT))",
" pygame.display.set_caption(\"GAME TITLE\")",
"",
" # Game state",
" self.running = True",
"",
" # Sprite groups",
" self.sprites = pygame.sprite.Group()",
" ",
" def handle_events(self) -> None:",
" for event in pygame.event.get():",
" if event.type == pygame.QUIT:",
" self.running = False",
" ",
" # Key pressed",
" keys = pygame.key.get_pressed()",
" # if keys[pygame.K_UP]:",
" # print(\"UP button pressed\")",
" # if keys[pygame.K_SPACE]:",
" # print(\"SPACE button pressed\")",
" # if keys[pygame.K_w]:",
" # print(\"W button pressed\")",
"",
" # Mouse events",
" # left, middle, right = pygame.mouse.get_pressed()",
" # mouse_x, mouse_y = pygame.mouse.get_pos()",
" # if left:",
" # print(f\"left button clicked: ({mouse_x},{mouse_y})\")",
"",
" def update(self) -> None:",
" self.sprites.update()",
"",
" def draw(self) -> None:",
" self.screen.fill(\"white\")",
" self.sprites.draw(self.screen)",
"",
" pygame.display.flip()",
" ",
" def run(self) -> None:",
" while self.running:",
" self.handle_events()",
" self.update()",
" self.draw()",
" self.clock.tick(FPS)",
" pygame.quit()",
"",
"if __name__ == \"__main__\":",
" game = Game()",
" game.run()",
"$0"
],
"description": "A comprehensive Pygame template using a Game class structure, including event loops, update, draw, and init boilerplate code."
}
}