Download pool/shard007/Python/0062786_models.py from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard007/Python/0062786_models.py
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard007/Python/0062786_models.py
-
curl -L -o 0062786_models.py https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard007/Python/0062786_models.py
1.65 kB
| from django.db import models | |
| from pygments.lexers import get_lexer_by_name | |
| from pygments.lexers import get_all_lexers | |
| from pygments.styles import get_all_styles | |
| from pygments.formatters.html import HtmlFormatter | |
| from pygments import highlight | |
| LEXERS = [item for item in get_all_lexers() if item[1]] | |
| LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS]) | |
| STYLE_CHOICES = sorted((item, item) for item in get_all_styles()) | |
| class Snippet(models.Model): | |
| created = models.DateTimeField(auto_now_add=True) | |
| title = models.CharField(max_length=100, blank=True, default='') | |
| code = models.TextField() | |
| line_nums = models.BooleanField(default=False) | |
| language = models.CharField(choices=LANGUAGE_CHOICES, | |
| default='python', max_length=100) | |
| style = models.CharField(choices=STYLE_CHOICES, | |
| default='friendly', max_length=100) | |
| owner = models.ForeignKey('auth.User', related_name='snippets') | |
| highlighted = models.TextField() | |
| class Meta: | |
| ordering = ('created',) | |
| def save(self, *args, **kwargs): | |
| """ | |
| Use the `pygments` library to create a highlighted HTML | |
| representation of the code snippet. | |
| """ | |
| lexer = get_lexer_by_name(self.language) | |
| line_nums = self.line_nums and 'table' or False | |
| options = self.title and {'title': self.title} or {} | |
| formatter = HtmlFormatter(style=self.style, line_nums=line_nums, | |
| full=True, **options) | |
| self.highlighted = highlight(self.code, lexer, formatter) | |
| super(Snippet, self).save(*args, **kwargs) | |