Storing and retrieving files in Blockly web

295 views
Skip to first unread message

Pete Hunter

unread,
Jun 3, 2018, 4:21:15 AM6/3/18
to Blockly

I want to use Blockly to do some calculations, and then generate text files (as opposed to exporting code to JavaScript, Python, PHP, etc.)


I can’t see an obvious way to create my own blocks to do this in Blockly, so using AppInventor (Version: nb168), I got storing and retrieving files to work, in a crude test app on my Android tablet.


In AppInventor/Designer mode, clicking Storage/File creates a “Non-visible component for storing and retrieving files. Use this component to write or read files on your device.”

Then, in AppInventor/Blocks mode, clicking the “File1” icon gives access to 7 “file type blocks”, e.g.

AppendToFile, Delete, ReadFrom, SaveFile, etc.


I don't want to use AppInventor, so is it possible to create similar “file type blocks” to use in Blockly Web?


I have limited programming knowledge, so would appreciate simple answers, please.

Thanks, Pete.


Andrew n marshall

unread,
Jun 3, 2018, 7:35:01 AM6/3/18
to blo...@googlegroups.com
Blocky does not include file or storage blocks. We expect app developers (like App Inventor) to build these if they are appropriate for their system.

It is possible to create such file type blocks for Blocky for your own app (which might be as simple as an extension of one of the Blocky demos), but it will require programming knowledge for both JavaScript and your target language. At that point, if you are the only user, it may be more efficient to write your script in the target language.

Is there a reason you do not want to write your script/procedure in App Inventor?


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pete Hunter

unread,
Jun 3, 2018, 5:10:48 PM6/3/18
to Blockly
Thanks for the reply Andrew.
I'm not sure if I understand correctly, but it seems that AppInventor needs the emulator or a device, in order to work i.e. it doesn't just work onscreen, in a browser, like Blockly or Scratch. You have to make an app (which then doesn't contain the blocks).
What I'm trying to do is make a block environment where users can click blocks into place, and generate G-code for CNC lathes. I've already got an app which I wrote in VB6 - now I'd like to make a version, using block programming, which should be easier and "more slick" for users. Also, CNC lathes have slightly different "dialects" of G-code, but my VB app can only output one kind. To overcome this limitation, I envision my user assembling blocks in one part of the screen, and multiple versions of G-code being generated simultaneously, and displayed in other parts of the screen - much like block environments where you can choose to see JavaScript or Python or PHP, etc.
I'm prepared to push ahead and learn JavaScript or whatever I need - I just want to be sure my goal is actually achievable before I start!
I hope this makes things clearer.

Andrew n marshall

unread,
Jun 4, 2018, 7:10:34 PM6/4/18
to blo...@googlegroups.com
This is absolutely possible  ...as long as you willing to work within the browser's security restrictions. The resulting files will be need to be manually "downloaded" one at a time, rather than written directly to the user's file system.

I should also mention there are a few Blockly based CAD environments out there. https://www.blockscad3d.com/editor/ and http://play.makerscad.com/ can each generate STL files.

If those don't do what you need (if you really need direct G-code), then I would start understanding what JavaScript functions are available to you. Attempt to construct a string and save it via a download dialog. Here's one approach.

That means the "file" contents are really just a string in memory, a JavaScript variable.  We have lots of "Text" blocks that can do a variety of operations on strings. If those are enough, you'll only need one new block to identify the string variable and initiate the download process.

Otherwise, you'll need to think about what blocks you want, and how they operate. They may operate on a specific variable in the JavaScript VM, not necessary exposed as a variable to Blockly.

Either way, you'll need to learn how to create a block and a Blockly app. We have a code lab that will walk you through all the steps. You'll learn how each block generates a string of code, and in your case, that code will be related to the download code I mentioned earlier.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.

Pete Hunter

unread,
Jun 4, 2018, 7:50:39 PM6/4/18
to Blockly
Thanks very much. The blockscad3d concept is great (but I do need G-code). You've given me plenty to think about - I'll look into it further...
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages