Introduction: Put Your Code in a Box, Properly Indented, on Instructables
When I first wrote code it was on punch cards and you could not indent. Now you can, and code that is not indented is not often worth the effort of reading. In Python if the indentation is not right the code will not run right. Still there is a lot of code published on instructables that is not indented. That is a shame. Fixing it is not hard.
Step 1: Fail
This instructable has been fine in the editor, but viewing on line the editor apparently has made some tweaks to the code and the formatting is now trashed, I have fixed several times but it will not stick. I will look into it and see if I can fix it. Anyone else got some tips. If I cannot fix it I will un-publish it. Continue if you want to imagine that it worked
Step 2: For Example
Here I put in a box and pasted the code in ( and the reader can copy it out, formatted ):
class RunningAve: """ proof and test in some other place tuples? no for long, could add and subtract from ends """ #TIME = 0 #COUNT = 1 def __init__( self, length ): self.val_len = length self.ix_now = 0 self.values = [ 0. ] * self.val_len self.ave_len = 0 # ----------------------------------------------- def add_val( self, new_val ): """ return new average after adding value value should not be a repeat """ self.ix_now += 1 # instead of this load the whole list with first value ??? if self.ave_len < self.val_len: self.ave_len += 1 # less smooth at begin, but not at 0 if self.ix_now >= self.val_len: self.ix_now = 0 self.values[self.ix_now] = new_val retval = 0. for val in self.values: retval += val return ( retval/self.ave_len )
Without the box the pasted code looks like this( it could be cleaned up with a lot of manual work for example:
- https://www.instructables.com/id/Arduino-Experiment...
- https://www.instructables.com/id/Computer-Controlle...
- https://www.instructables.com/id/Arduino-Trumpet/
)
class RunningAve:
""" perform running average """
def __init__( self, length ): self.val_len = length self.ix_now = 0 self.values = [ 0. ] * self.val_len self.ave_len = 0 # ----------------------------------------------- def add_val( self, new_val ): """ return new average after adding value value should not be a repeat """ self.ix_now += 1 # instead of this load the whole list with first value ??? if self.ave_len < self.val_len: self.ave_len += 1 # less smooth at begin, but not at 0 if self.ix_now >= self.val_len: self.ix_now = 0 self.values[self.ix_now] = new_val retval = 0. for val in self.values: retval += val return ( retval/self.ave_len )
Some people get around the problem of pasting in code with screen shots, but you cannot scroll these, include more than one screen of code, or let the reader copy formatted code out of the instructable, the box lets you do all of this.
Step 3: Tools and Materials
- Pro membership ( just write a good instructable that for inscrutable reasons is favored by the site managers and get one free ) You need this to get access to the html editor feature.
Step 4: Do It
In the editor put in some of your regular text above and below where you want the boxed indented code, you do not need much text but it is hard to insert before or after the box if you do not do this.
Then press the “</>” icon. This will take you into a mess of HTML for the page. Be careful it is easy to loose your way. Then insert the text in the attached file ( I cannot put it in a box for you to copy because the HTML does not like HTML inside of it - at least as far as I have figured it out, plus the editor likes to change things around. )
Then exit the HTML using the "</>" icon again. You should see the box. Now that you have it you should be able to type in it, cut and paste with it, publish some descent looking code with it. Done.
And Hey! Instructables why not make this easy for everyone.
11 Comments
7 years ago
I think I got some of my info from https://www.instructables.com/id/How-to-Enhance-You... . It is nice, but if you look at it you can see it is somewhat messed up, probably by the automagic features of the site. Would be so nice if instructables made sure all this stuff worked. It cannot be that hard can it?
Reply 7 years ago
Yeah I've got to go over it again but thanks for the credit! I used overflow-y, which I believe isn't supported anymore.
I completely agree that Instructables could support way more html without messing up any content. I actually went through all of the useful HTML tags for the ible you linked above and most didn't work
3 years ago
I have given up on instructables fixing this. In general they have constant problems debugging their own site; stuff is broken and stays broken.
In any case I now recommend that you put your code at github ( or gitlab..... ) and link to it.
You can read code ( and see other types of files ) without any download or messing about.
Reply 3 years ago
Meh.
Agree re linking to code &c. at github.
I still wanted to inline some "terminal"-style text in an 'ible.
This is working so far: add a code box; paste text; save; _never_ edit the step again.
That means only one (multiline) code box per page. Because touching the 2nd kills the 1st. I aslo attached files of the same text, so that if it gets screwed up someone can still read from the file and I can copy from the file to fix the 'ible.
Steps 2&3 here: https://www.instructables.com/id/HC-05-Serial-Configuration-Over-Bluetooth/
Reply 3 years ago
Thanks for the info. Glad you have not given up on them, but I would need to see
more signs of life, before I put effort into fixing the site.
7 years ago
Box is working again, edited, then waited for auto save and quit. Is this it? Not sure what is happening, first it is fine then automagically not. The save button may be part of the problem.
Reply 3 years ago
...four years later...
Reply 3 years ago
see my new comment, I have given up on posting code to instructables.
7 years ago
why not just post the source files as an attachment?
Reply 7 years ago
It separates the code from the discussion of it and is more difficult for the reader to access among other reasons. But attaching all the code is always good. Better if well commented, but rarely seen.
7 years ago
"puts code in box"