10

The vast majority of code shown under the , , and tags is Python; the most commonly-used API for these libraries. It would be helpful if we could add automatic Python syntax highlighting for these two tags.

Currently no syntax highlighting is set by default, so we get un-highlighted code unless the author happens to include a tag with their post, or adds <!-- language-all: python --> to their code manually.

Edit 1: I finally found a post on the Tensorflow tag that uses both C++ and Python code. I tried out both formatting tags on the C++ code blocks and they look the same. I am sure there are some fringe cases in which they might look different, but I think overall defaulting to Python syntax highlighting on these tags is safe.

I tried to show some example code blocks, but it looks like syntax highlighting isn't enabled in meta.

Edit 2: Note that many questions with the , , or tags use Python code, but really have nothing to do with the Python language or syntax, and thus appropriately are not given the tag. In these common cases, I would say that the coder should not include the tag just because their post includes Python code, and for that matter a solution that relies on the poster to know to do something discussed in a meta thread once upon a time is not a solution at all.

Edit 3: I found a couple of questions on the tag that show shell commands for installing libraries, running scripts, etc. Generally these look the same with Python syntax highlighting or bash syntax highlighting, so I think this is another safe case. I would really love it if somebody with the authority to set default syntax highlighting could just pick one (I highly recommend Python), and set it for the , , and tags. Otherwise I can continue my crusade of manually adding <!-- language-all: python --> to every other question I find that did not include the tag.

P.S. relying on including the python tag, in my opinion, just clutters this tag with a bunch of questions that are in no way related to Python programming, and leaves us exactly where we are now, with tons of un-highlighted code gracing our tensorflow tag. See edit 2 above.

14
  • 3
    Would that cause any issues with people using the TensorFlow C API, or other language bindings?
    – Brad Larson Mod
    Commented May 22, 2017 at 17:32
  • 1
    In my experience that's the vast minority, and you can always overwrite with a manual language tag. I'd say it's preferable to never having syntax highlighting.
    – Engineero
    Commented May 22, 2017 at 17:35
  • 13
    Why not just add the relevant language tag? That should happen anyways, shouldn't it?
    – Baum mit Augen Mod
    Commented May 22, 2017 at 21:17
  • 2
    Because a lot of times your question has nothing to do with Python, but the code that you use to illustrate the problem you are seeing with TensorFlow is Python code. Adding the Python tag would clutter that question space. And if your question is why not add the syntax tag <!-- language-all: python --> to your question, the answer is because nobody ever does. The proposed change would clear up the editing queue a bit and make questions more readable by default.
    – Engineero
    Commented May 22, 2017 at 21:28
  • 1
    I should also add that most people do not add the Python tag to their question because they are not asking a question about Python, but, again, their question uses Python code to illustrate their problem.
    – Engineero
    Commented May 22, 2017 at 21:29
  • 1
    @BradLarson I finally found a question on that tag that used a lot of C++ highlighting. It looks like applying the Python rules to C++ code gives you the same result. I tried both, they looked the same. I bet there are some fringe cases where this is not true, but overall I think this is a safe improvement.
    – Engineero
    Commented Jun 21, 2017 at 14:31
  • 2
    And when people do add <!-- language-all: xxx --> to a post it's almost always because they used the "add snippet" feature, which tags the code as JavaScript and tries to present it as a runnable program (which it is not). I've had to remove the <!-- snippet --> tag and fix the language on over a dozen C# Unity questions in the last couple of weeks alone. Commented Jun 21, 2017 at 17:05
  • Are there many cases where a framework tag triggers syntax highlighting to some other language? I only know that node.js does this, which is more justified.
    – E_net4
    Commented Jun 21, 2017 at 17:07
  • 3
    Any chance of a resolution on this? I can keep adding syntax highlighting tags and filling up the peer review queue for 2 reputation points a pop, but I would rather not. It makes long code blocks much more readable, and I have never come across a post under the tensorflow tag that would be hurt by auto-applying Python syntax highlighting.
    – Engineero
    Commented Aug 23, 2017 at 16:42
  • 1
    Do not just ping everyone who has commented on your post. That's annoying and unwelcome. Editing it has already bumped it up into the "active" Meta posts. Someone will see it.
    – Cody Gray Mod
    Commented Aug 23, 2017 at 16:53
  • @CodyGray that's news to me and good to know that editing it puts it back in an active queue, thank you.
    – Engineero
    Commented Aug 23, 2017 at 17:00
  • 2
    On the home page, you'll see that your question is currently at the top.
    – Cody Gray Mod
    Commented Aug 23, 2017 at 17:01
  • 1
    Agree, and has just suggested to do the same for pyspark and sparkr tags: meta.stackoverflow.com/questions/362624/…
    – desertnaut
    Commented Jan 29, 2018 at 1:35
  • I have been trying to get this prettifier and another default prettifier for Julia set for way too long now. This might be a separate meta discussion, but the process for supporting new languages that do not currently have a default prettifier seems broken. Julia and TF have both been out long enough that the lack of support seems to imply SO is unable to change with the times. I don't think that's the impression that anyone in this community wants to give.
    – Engineero
    Commented Jul 25, 2019 at 17:22

2 Answers 2

6

We should set syntax highlighting to Python by default for the tensorflow, tensorboard, and keras tags

No, we obviously shouldn’t. These frameworks are not exclusive to Python, and at any rate all questions should be tagged with a programming language anyway.

So setting a preferred syntax highlighting for these tags (a) degrades user experience for users of other languages, since the conflicting highlighting hints will lead to incorrectly-inferred syntax highlighting, and (b) it provides no added value even for Python questions, except low-quality questions (which are tagged incorrectly).

6

This question was initially marked as and the default syntax highlighting changed to Python for each of the requested tags. As part of Stack Exchange's change to using highlight.js, the overall behavior of the syntax highlighting changed with respect to resolving situations where the question's tags result in more than one default syntax highlighting language, which necessitates revisiting that prior resolution.

Unfortunately, when the default syntax highlighting languages for the tags on a question don't resolve to a single language, Stack Exchange's implementation results in telling highlight.js to use auto-selection over all available languages for each code block. This tends to end up with a large number of questions and answers with code blocks which are highlighted using the wrong language. Using the wrong language can result in the code being confusing. The behavior tends to be substantially contrary to user expectations, as most people expect that the syntax highlighting for the question's primary language will be used. Overall, it's better to have no syntax highlighting, rather than wrong syntax highlighting.

In general, since the change to Stack Exchange's implementation and use of highlight.js we've handled minimizing such disruptions by not assigning a default syntax highlighting language to tags which are not a primary language tag and relying entirely on the default syntax highlighting language associated with the primary tag. Not assigning a default syntax highlighting language to non-language tags is a higher priority when the secondary tag can be associated with multiple primary languages, as is the case with the tags here.

Ideally, Stack Exchange will change their implementation such that when there are multiple default syntax highlighting languages defined by the question's tags, then the auto-detection of syntax highlighting language for the question ans answers will be constrained to the resulting list of languages.

Until that happens, I have returned the default syntax highlighting language for all three of the tags, , , and , to <none>, which will allow the primary language tag applied to the question to control the selection of the default language used by highlight.js for syntax highlighting.

7
  • 1
    Thanks for undoing the syntax hinting. That said, as fas as I am concerned the situation does not require further action in the future: even with better language detection there’s zero benefit to associating these tags with any syntax highlighting language. This request never made sense. Commented Apr 19, 2023 at 19:18
  • I don't really understand the reasoning here. You say that no highlighting is better than wrong highlighting. But by setting these tags to none, the library has to guess (in absence of a main language tag), which means that more questions will now get wrong highlighting. Of course there are also questions that will now be guaranteed correct (like c++ tensorflow), but given that the overwhelming majority of questions in these tags are using Python (as demonstrated in the feature request), statistically this is surely going to make things worse overall.
    – Marijn
    Commented Apr 20, 2023 at 11:24
  • @Marijn The tags under discussion here aren't language tags, so don't have an inherent language which users should be expecting to be used for syntax highlighting any code which they are providing. If they are including code, then they should add that code's language tag. When a user includes an actual language tag on the question, then there's a very reasonable assumption that that language will be used for syntax highlighting. When they don't include a language tag, they might desire a particular language to be used, but they haven't actually specified one.
    – Makyen Mod
    Commented Apr 20, 2023 at 16:01
  • Ideally, the list of languages associated with the tags on the question would be used as the ones from which the autoselection is done (with, perhaps, an upward adjustment in relevance for actual language tags). Another potential, implementation would be to use auto-detection all the time, but substantially bump up the resulting relevance of each of the languages associated with tags on the question, with, perhaps, a higher bump for actual language tags. If SE used the list of associated languages, then we could associate tags that tend to be a particular language.
    – Makyen Mod
    Commented Apr 20, 2023 at 16:01
  • Re adding language tags: the feature request here makes the valid point that these questions are not about the language but about the framework and therefore the language tag should not be added, it would just clutter the questions without adding anything except for setting the syntax highlighting (after this change), which is Python anyway in almost all cases. You didn't address the statistics argument, I don't think the added value for c++ users outweighs the mis-highlighting introduced for posts using Python.
    – Marijn
    Commented Apr 20, 2023 at 16:27
  • @Marijn If the person is including code, then it's about code in a language and the language tag should be included. If it's strictly about the framework, without referencing any code, then why does the default syntax-highlighting language matter? The issue of assigning syntax highlighting languages to tags which aren't languages is an issue of having the least conflict of expectations. If a language tag is included in the question, then that is the language that should be used for syntax highlighting, unless there's substantial indication that something else should be used.
    – Makyen Mod
    Commented Apr 20, 2023 at 16:56
  • While it would be great to have the system use all defined, default, syntax-highlighting languages for the tags supplied, that is, unfortunately, not what happens, at this time. It is how it used to work when this was first looked at, but how SE handles things changed. If SE, ever, changes back to a reasonable handling of having multiple default, syntax-highlighting languages for the included tags, then it will be quite reasonable to define a default, syntax-highlighting language for non-language tags where the tag is reasonably used with more than one language.
    – Makyen Mod
    Commented Apr 20, 2023 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .