I'm creating a Django website for game developers to share code, which means I need to be able to save properly formatted code to a database, as well as being able to retrieve and display it.
Is there anything built into Django for this, or do I need to use a third party library?
I've played with a Django pagedown widget, which is modeled after StackOverflow's markdown editor, but I've had some trouble with it, and I'm not positive if that's is going to accomplish everything I want by itself.
Has anyone actually used the pagedown widget, implemented functionality similar to this, or know of another library that would work?
For storing the code, you can use any database that supports text types. That is, you would use a TextField in your model.
Now you need to decide how that raw text will be displayed/formatted.
This depends on what you mean by displaying properly formatted code, but I'm going to go ahead and just say you should check out Pygments. It is a syntax highlighting library that can be easily used w/Django.
Pygments works by your specifying a lexer (read: language) for it to apply styling to the target text. Whitespace will be preserved by your database. Everything else formatting-related is purely subjective. You'd presumably specify a fixed width font, a size, etc in the view and pass it to the context as you would any other DB-backed object.
Django Rest Framework's tutorial actually makes something very similar to what you are describing and uses Pygments, so I'd recommend checking that out for concrete usage examples.
For formatting code exclusively, I would avoid any markdown flavors like the one you listed, as code could contain characters that have special meaning in markdown, thus you would need to escape the text, and that defeats the purpose of markdown.
Related
I have some text fields in my Django model that are filled by a script, with values in English (the list of values is known).
But the app is actually made for Russian clients only. I'd like to translate those fields into Russian, and here comes a little question. These values are taken from an API response, which means I should check the value to translate it. What's faster: to check and translate fields in template or to make extra fields and translate strings in the Python script?
The problem is overhead of compiling Templates when rendering. So the more complicated the template gets (method calls etc), the performance tends to get slow (like py files are converted to pyc). Django has template caching but that also is limited (I don't know how much). I have faced performance issue because of lot of logic in templates. Plus its always good to have a dumb client (template). I will prefer the Python approach because of the idea to keep client thin and not because of the performance gap. Plus if tomorrow you need to add one more language then changing templates is always going to be difficult then server.
I use Sphinx and the autodocs feature to ensure we have good docs in our project.
So I'm familiar with info field lists and I'm familiar with using cross-referencing in our docs.
However, when writing docstring for a method or function I find it useful to refer to their parameters in the text. But there doesn't seem to be a structured way to do this.
We could say e.g.
Use ``name`` to set the username
but that has no structure, requires you to remember what style you used for that and if you change style you have to hunt down and kill all the incorrect styles.
:param: doesn't work outside of a info field list so you can't write
Use :param:`name` to set the username
I've seen some projects use :parm: but that isn't documented and doesn't seem to work. So they must have some customisation
I could use generic_roles but that seems like me working around a problem that I'm sure others have encountered.
So hopefully I've just missed something blindingly obvious.
You can write your own extension using autodoc-process-docstring - it's really simple.
Have the extension search for :param: and replace it with your choice of style.
I'm adding a simple web interface to an already existing piece of software; web.py fits the job just right and that's what I'm using. Now I'm researching what templating engine to use and came down to two alternatives: either using web.py's own Templator or using Jinja2.
I already have both working in the app and I'm writing some very simple templates in both to explore them. I must say I find Templator easier to read, that's probably due to me being a programmer and not a web designer (who would probably find Jinja easier?).
While I'm only generating (non compliant ;) ugly HTML pages now, I'll also use the templating engine to generate emails and good old plain text files.
Both softwares are "fast enough" for any practical purpose, I'd like to ask people who used extensively one or the other or both what are their strengths and weaknesses in the areas of ease of use, code cleanliness, flexibility, and so on.
Taking a quick look at Templator (which I have never used) and comparing it to Jinja2 (which I have used somewhat extensively), I'd say that the two are pretty similar ... but Templator is closer akin to Mako than to Jinja.
Mako and Jinja both support:
Template Inheritance (You can have a layout that all your pages inherit from)
Whitespace control
While Mako and Templator both support:
Embedding "safe" Python in your templates.
All three support:
Adding to the template context (functions, objects, variables, the works)
Defining functions to encapsulate re-usable pieces of functionality in your templates (Jinja calls them "macros".)
Conditionals and loops
Setting and getting local variables.
Expression evaluation
Caching the compiled bytecode to speed up future execution.
Templator supports on weird thing that I don't believe either Jinja or Mako does:
Setting attributes on the compiled template object from within the template code. (Speaking frankly, actually making use of that feature seems like the wrong thing to do. Any flags that your template could determine need to be set from what has been passed in by the context should already be set by your application code.)
Jinja takes the template code and compiles them to Python bytecode, but it does that for everything, rather than passing through strings to the Python interpreter to use safe_eval. By doing so Jinja2 is theoretically immune to certain types of attacks on the template level (But when you have hostile input from your templates you generally have a much bigger problem).
As for the rest of it, it's all pretty much up to your preference for syntax.
What was hard for me in Templetor is template inheritance. Instead of simple concept of blocks which is present e.g. in Jinja2, you have to select the base template once in the app code, then do the weird attribute setting in the actual template while accessing it in the base template. Still you have problems if you need more than one “big” block like the page body.
Real blocks are much more elegant, and the flexibility of Templetor's “real” Python is not really necessary, while it probably can be unsafe.
I'm looking for a way to translate my Django project. Built in mechanism provided with Django is great, but has several weak points which made me go looking for an alternative.
Project owner must be able to edit every translation including English (original translation). With gettext it is possible to edit translations with tools like Pootle, but the original strings stay hardcoded inside file sources or templates. There is no way that product owner can change them.
Possible solution is to make gettext translate some unique identifiers, and just translate them to all languages including English, like this:
_('form_sumbit_button')
But this makes tools like pootle almost impossible to use for translators.
Question: are there any tools for Django project translation that could fit my needs?
If you use some message IDs, they would either be incomprehensible ("message_2215") or you'd be forced to synchronise the message IDs to the actual messages ("Please press any key" = "please_press_any_key" => "Any key to continue" = "any_key_to_continue"). Either way, real strings are better for the programmers and for the tools.
However, if you employ a separate proof-reader for your strings, you can do the following:
Create an English "translation" file (yes, this works)
Let your proof-reader "translate" from English to English using Pootle or any other tool
Make sure your programmers keep that translation file untranslated by updating the strings in code.
(optional) Create a way to deploy translations independently of your main code so you can fix a typo quickly.
You may be able to use Pootle with the _("message_id") approach, depending on how easy Pootle is to customise (I don't know the internals so I can't say, but IIUC it uses Django where template changes are usually straightforward).
For example, Pootle's translation screens have "Original" and "Translation" sections; you could perhaps adapt the templates to show, under the "Original" section, a "Reference" section which displays some canonical translation using a specific reference language (e.g. English).
Or you may be able to use Pootle's alternative source language functionality, without needing to customise Pootle. You could store the canonical versions of the translations using an unused language code (or a made-up one).
Using identifiers is definitely possible with Gettext and there are tools which support this. However it might be unusual for some translators as they are used to downloading only .po file for offline translation, what does not work with monolingual translations.
For example Weblate supports monolingual Gettext files just fine (I'm author of this tool): https://docs.weblate.org/en/latest/formats.html#monolingual-gettext
I'm going back over an old project where I added preprocessor functionality to Essence' and I realised that my previous solution of writing a domain specific language and associated lexer/parser was overkill.
Instead I just need to be able to embed dynamic language code into the file, isolate it at runtime, eval and insert the results. In other words very similar to the PHP model of inserting dynamic code into HTML. I'd rather not use PHP as Python is much easier to distribute as part of a larger project (IronPython or Jython)
So the question goes, how best to implement something like the following:
<code>Python goes here</code>
Lots of essence <code>Python</code> prime code goes here
I don't want to have to alter the structure of the Essence' file (if I remove all the code blocks everything left should be able to be syntactically correct. It needs to be able to insert text in place of a code block like PHP.
Finally security wise I'm not bothered about code injection, as it would be the user themselves choosing the file to execute although if there were security benefits to one model over another with no extra costs that would obviously be good.
Cheers in advance
Your best bet is to use one of the already made (and battle tested) Templating Engines. The two big ones that I've used are Mako, and Cheetah. They allow you to embed code right in the page, and are mostly used as the View in an MVC architecture.
If you feel that using one of those engines is overkill for your project, here is a small tutorial on how to implement basic templates yourself. Keep in mind that the example will need to be modified to suit your particular project/needs.