Consistently use separate file with python code?

183 views
Skip to first unread message

Olof Paulson

unread,
Jun 2, 2021, 6:43:43 AM6/2/21
to brython
I am struggling with a very basic thing( i think)

Am unable to understand how to consistently separate the ’python-code’ into a separate file...

The majority of my use is creation of tutorials at this moment, and as such I am trying to give a python experience, and remove as much html/ javascript from sight as possible.

I am not sure exactly why I am unable to separate the python code from the html/ javascript in the example I have ’stripped down’ from Pierres code on the Brythonpage...

What I would like to achieve is two files 
index.html and index.py(containing the python code)...

But not sure what I am missing...when I separate it...it just won’t run...
https://scrimba.com/scrim/co315493b996b9d5b0384aa2b
Getting a general understanding for what needs to be in the index.py file and what glues it to the index.html , feels like it’s where I’m falling down, but not sure...

With that said, the idea of writing the code in the browserwindow is warming on me...conceptually...

also I don’t understand why I am unable to execute Regular Turtle from the index.py file, but only able to do ’procedural version’...probably same-ish issue ;-)
as in my latest ’version of the turtle game’
https://scrimba.com/scrim/c6dQLLsr
When all the code is in the index.html file, regular version works...

Sorry for a likely very basic question

Olof

Ray Luo

unread,
Jun 3, 2021, 3:48:36 AM6/3/21
to brython
Hi Olof,

I do not have a definite answer to this topic, however I suspect there might be some not-yet-clear side effect caused by how scrimba's multi-frame web page handles Brython's heavy usage of indexDb. In my own experience, my scrim also tend to break during first several attempts, but it might eventually work after many refreshes. This happens just now when I pasted a but-it-works-on-my-machine (TM) "CSV sample for Charts.css.py" into Scrimba. Eventually it works. https://scrimba.com/scrim/cqW4kmuG

I would suggest you to also run your scrim on your local computer (by serving them with "python -m http.server 8000" and then visit http://localhost:8000), during your course development phase. That way, you would at least have a solid starting point to know your source code would be correct, before you start westling with Scrimba.

Regards,
Ray Luo

Olof Paulson

unread,
Jun 3, 2021, 4:39:02 AM6/3/21
to brython
Wow Ray! Holy Shit even!!!

You got csv reading file to work!!! atleast on your end...

I can see the graph when I load the scrim...but just misery when I try to execute myself?
just get error/ after error saying ’Missing cw1’ in the minibrowser...and then it just seems to refuse to execute at all

When you say eventually, what does that mean and what magic do you do? 

When loading scripts I have seen scrimba show message cw0.... so I don’t know f it’s gateway/server or something, and there are conflicts

I get this message from Firefox: ( Scrimba issue?)
Error 1033 Ray ID: 65979875bd55d13f • 2021-06-03 08:36:45 UTC Argo Tunnel error
What happened?

You've requested a page on a website (cw1.scrimba.com) that is on the Cloudflare network. The host (cw1.scrimba.com) is configured as an Argo Tunnel, and Cloudflare is currently unable to resolve it.

What can I do?

If you are a visitor of this website:
Please try again in a few minutes.

If you are the owner of this website:
Ensure that cloudflared is running and can reach the network. You may wish to enable load balancing for your tunnel.

Cloudflare Ray ID: 65979875bd55d13f


Olof Paulson

unread,
Jun 3, 2021, 8:05:11 AM6/3/21
to brython
What I have learned today:
1. I can get your scrim to run by bypassing the csv import and setting the sample_data to the hardcoded list of lists in main.py 
2. brython 3.9+ versions seem to crash?? @3.8 works...(with css.py charts) @3 or @3.9 do not...I have Zero idea what that is about..have contacted Scrimba for assistance

3. Apparently Scrimbapeople misconfigured some ’tunneling thing’ on cloudflare causing more than alittle issues...
They claimed to have fixed it ??

possibly the cw0, cw1 (workers?) are related to issue. POssibly a new one is instantiated/ routed?? when I have a secondary connection to scrimba i.e ? first tab with scrim = cw0, second tab cw1...etc...since I had 5 tabs with scrims, maybe that had some effect?

I will try to do some playing from here..and see where I land

Have exactly Zero idea how you got your csv read to work ;-) or maybe it was reading some artefact in your cache? 
Also don’t understand what you wrote about server...I’d love to do it..but don’t know how...sorry :-(

Olof



Victor Norman

unread,
Jun 3, 2021, 5:29:18 PM6/3/21
to brython
I'm late to this discussion, but perhaps my basic set up will help you. My index.html looks like this:

```
header stuff here...
<body onload=brython(1)>

  <script type="text/python">
  import main
  </script>
  <!-- <canvas id="myCanvas" width="400" height="110"></canvas> -->
</body>
```

And my main.py looks like this:

```
from browser import document, html, timer
... insert code here ...
canvas = html.CANVAS(width=WIDTH, height=HEIGHT, id="myCanvas")
document <= canvas
# Call main code here to draw on canvas
```

Thus, there is no python code in the html file and no html in the python file.

Olof Paulson

unread,
Jun 3, 2021, 5:40:49 PM6/3/21
to brython
Made this graph on Covid reproduction: https://scrimba.com/scrim/cWWyr8UJ
How would you approach the graphing?

In the best of worlds I would like to animate the graph so that it draws a graph of one generation at a time to display , basically redrawing a new graph for each generation.
I was unable to make that happen, possibly due to scrimba, not sure , tried a few different alternatives (timer, time.sleep)

The main issue is that visually the ’non-indian’ variants are so far below, that they cannot be viewed reasonably, after generation 4-5...
This is why I made two graphs...
Log scale would be one alternative( does it exist in css?).

all improvement ideas welcome :-)

Ray Luo

unread,
Jun 4, 2021, 2:18:36 AM6/4/21
to brython
Wow, Olof, glad to see you able to come up with some charts with real world meaningful data! I might borrow your charts into Charts . css . py 's sample. :-)

Regarding to how to get csv reading work on Scrimba, I honestly don't know. But I strongly suspect it is something at Scrimba's end on how they handle a special javascript Brython.js which has some presumably unusual behavior. :-)

Bottom line, this is the csv loading sample that I hosted as a normal static website, and it works reliably. https://rayluo.github.io/charts.css.py/#read_csv  But the same sample does not always work on Scrimba.

Andy Lewis

unread,
Jun 4, 2021, 1:12:36 PM6/4/21
to brython
Hello Ray and Olof

That's an interesting idea for some data, Olof.
Here is the data shown using brycharts: https://scrimba.com/scrim/cvQLm4cr
(I have done graphs of 4, 5, 6, and 7 generations, but as you say by the time you get to 7 generations the first 4 generations just look like a straight line at zero. Giving an option to use a log scale is an interesting idea which I might incorporate into brycharts.)

As you will see in the html file, I am using Brython 3.9.2 in Scrimba with no problems, so that particular issue seems to have gone away, for me at least.

However, there are still problems with brycharts in Scrimba.  It only works up to commit 715a65f, so that has to be specified in the html file. That was before I implemented the title, so that doesn't appear on these graphs, and also before tooltips, so hovering doesn't work.

I don't know what Scrimba is objecting to in later versions, I will try to look into it but it is not easy because the "this.addEntry is not a function" error message is not very informative.

Scrimba looks a very useful platform though, so I feel it is worth making some effort to get our packages working.

Best wishes

Andy

Ray Luo

unread,
Jun 4, 2021, 2:46:47 PM6/4/21
to brython
Hi Andy, I do not think that issue has gone away. Even loading the scrim page you shared, I still see lots of "this.addEntry..." error on the console. And that is also my experience with Scrimba. My scrim page could eventually (TM) work at one day, but then the next day if I change it even just a bit, the reload might trigger those weird errors again.

I still suspect it is not about how we (as Brython-based app's developer) did wrong. It is about some underlying issue on how Brython could be run on Scrimba.

I fully agree that Scrimba can be a very useful platform for online education or generic code sharing, especially for a browser-based software like Brython. Therefore, I wonder if Pierre could have time and interest to try experience Scrimba (and its issues) first hand, and then see if he could pinpoint the issue.

Regards,
Ray

Olof Paulson

unread,
Jun 4, 2021, 3:59:48 PM6/4/21
to brython
@Victor, Thank you for your suggestion!
I have been trying to play with the charts these last days, and haven’t done turtle for a few days.
Or did you mean to use your code for ’refreshing’ the graphs...not 100% sure?
Will look into it shortly...
Need to try to redo what I tried with charts.css with Andys Brycharts...
Unfortunately, I am not sure I am able to fit my code seamlessly into your snippets( another way of saying I am too useless to figure out how to make it work as per your snippet ;-) )

My problems with the Turtle code are
1. there are two versions of Turtle
-regular and
-procedural
So far I am only able to run Procedural using the index.html + ’main.py’ type set-up, i.e using main.py as the python code file

The only way I was able to execute ’regular’ turtle was by having both in the html file...
Feels like you are saying that is how your code will help ;-)
If you can spoon-feed me alittle more snippets to make it run I would really appreciate it.

Running procedural seems to limit functionality somewhat, and it’s not  really ’full’ syntax that people would normally use for turtle...Useful for ’super beginners’ but maybe not as relevant for  beginners...

I will forge on after the weekend, I hope..

@Andy 
Thank you so much for sharing that, I did Rays package first since I could get it to run, and when I tried yours I crashed on the linecharts. Maybe it was as you say the versioning...
I will dig back into it...I love both your packages...ebing held back , again by my lack of imagination, and inability to execute correctly

I agree somewhat with the log charts as a nice feature, but I am not sure how common it is...( i.e is it worth for you to put effort into?)
For adoption, considering your Teacher background, I think the coordinate plane graphs you mentioned/ showed may have more ’legs’( I’m thinking of simulations for schoolkids/ math charts that the students can play with the math themselves...) sort oof in the realm of geogebra but..Python! 

Specifically I was thinking about maybe Demonstrating the difference between linear and exponential growth, using some linear examples, and then using something ’in the news’ like Covid R-values ...The purpose being to expose our( as humans) complete lack of intuitive understanding of what the effects of exponential growth are, since our ’daily world’ is more or less linear, and our intuition just leads us astray...
An example
walk 5km/h for 7 hrs --> 35km
Get on bike and double speed --> 70km  ...2x

R-number of 2.5 - for 7 generations -->244 infected
Double r to 5 --> what does your intuition tell you is result....?
Alot closer to 2x than 64X probably..for most...64x seems impossible from any doubling, except for the most badly log-damaged ;-)
What can we learn from that? (it’s a thought anyway...)

@Ray: 
I tend to agree. 3.9 seems to have some issue...that triggers when it is imported to Scrimba...
with earlier versions the console is calm, but with 3.9+ brython version , Scrimba prints out anew lie about cw?? something, part of the text in green( had never seen that before running Brython on Scrimba..)
I am basically clueless...

Your description of ’works sometimes/ sometimes not’ worries me... one hang/crash and many students will give up...
I have experienced times in the past where  I have had to restart my computer to ’unlock some code havoc in scrimba’ where it seems to chache some parts that then messes everything up...
Again I don’t know why...
Sindre- the brains behind Scrimba, built it...using his own language IMBA...because...of course...
I don’t doubt he is talented...but when you make up your own lang....there is a chance you may not have all the support structures and tools, you will have using established langs...

@Pierre
If you find Scrimba interesting as a platform to further Scrimba I am happy to support by making tutorials, as we are able to show off more ’stuff’
I belive it can have some legs and If I can get the tutorials out of the ’text based trap’ and start introducing some of the sexy stuff
Datascience + graphs
TkInter
OpenCV
ML
Turtle( to some extent)
Building games (Hangman, tictactoe, maybe pong or something) with output onto html minibrowser...
I think we can put brython more on the map...

Right now Scrimba guys are putting effort and focus only on frontend developer path , javascript/ react/ Css etc...

My python course is very popular, but they don’t push it, nor do they get any income directly from it( since it’s free, and not supported)
If we can bring people to tutorials, Brython may get more exposure...

On a personal level, I am alittle lost with several specific implementation points brython/ Scrimba and would greatly appreciate any assistance.

also as Dominik mentioned in our meeting, I think we could try to introduce scientists to coding with graphs using a browser based environ and focusing on data-science for Brython...maybe
I thin there is potential, allthough I am not sure how close browser based jupyter notebooks for this maybe...or Pyodide, WASM as @Kiko mentioned the other day

Speed of execution/ faster loading + the access to c-module python functionality ...feel like important areas to capture the Python/ datascience/ financial data ’wave’

I am very happy to have met you all and apologize for spamming with so much stuff

Have a great weekend!
Olof

Andy Lewis

unread,
Jun 4, 2021, 5:42:53 PM6/4/21
to brython
@Ray

Hmm, I just found that with the scrim I linked to in the last post, when I reloaded the page it went back to a previous version - I haven't worked out how to "save" in Scrimba, so that changes will stick. There doesn't seem to be a "Save changes" button.  And you are right, with the previous version the "this.addEntry..." errors were there.

Anyway I have copied it to a new scrim: https://scrimba.com/scrim/co157465cac63afe0e591f5ab

And this one does seem to have "stuck" - no matter how often I reload the page it remains the latest version. And with this one, even though it is Brython 3.9.2, I never get any errors.

Would you mind trying this one and seeing whether you get errors?  If you get errors and I don't, then there is definitely a strange issue.
(It might be slow on the first run, depending on what is going on with the IndexedDB.)

Best wishes

Andy

Ray Luo

unread,
Jun 4, 2021, 7:03:01 PM6/4/21
to brython
Andy,

Scrimba might be doing the auto-save thing, so I assume the scrim's author need to signed in before attempting any changes. 

Your new scrim works smoothly this time.

But I still think there is something going on on the underlying Scrimba+Brython compatibility. For example, my scrim (https://scrimba.com/scrim/cqW4kmuG) was using Brython 3.8 and it worked yesterday when I shared it with Olof, and Olof also replied saying he also saw it working. Today I noticed your scrim is using Brython 3.9.2, so I also tentatively changed mine to use Brython 3.9.2, and then later 3.9.1, 3.9.0, in an attempt to find a latest working Brython version. All of them ended up with some weird error messages. And then I rolled back to Brython 3.8 which worked yesterday and - guess what - my scrim is now still failing. #SMH

Regards,
Ray

Andy Lewis

unread,
Jun 5, 2021, 4:35:26 AM6/5/21
to brython
Ray

Well I'm glad my new Covid scrim seems to be working fine for both you and me. (@Olof, it is https://scrimba.com/scrim/co157465cac63afe0e591f5ab)

>> Scrimba might be doing the auto-save thing, so I assume the scrim's author need to signed in before attempting any changes
I assumed Scrimba was using auto-save as well - but I was logged in while making the changes to my previous scrim, and they didn't get saved. But I just tried making a change to that scrim this morning, and it did get saved...

With regard to your scrim, when I first go to the page (or re-load it) I get "AssertionError" messages and a blank pop-up window.  But if I close that window and click "Run" then the graph appears in the pop-up with no error messages in the console.

It's all a bit mysterious.

Best wishes

Andy

Ray Luo

unread,
Jun 5, 2021, 5:48:02 PM6/5/21
to brython
Well, I'm writing to share my observation from my debugging my scrim's "AssertionError", for what it's worth.

It turns out, the normal "open('sample.csv').read()" operation would silently give you empty content, when running on Scrimba. In fact, if you enable your browser's debug console, you will see no request/response for accessing the "sample.csv".

That was actually the same issue Olof brought up a while back. Pierre helped coming up with a File Mock workaround which would be based on browser's local storage. Here in my sample, I feel that workaround would be an overkill and take away my audience's attention, so I go with an even simpler workaround, by just fallback to hardcoding data. Now my Scrim "works".

That being said, I still do not understand, and can not reproduce, how the csv loading was working on Scrimba when I worked on it the first time several days ago, and at that time Olof also confirmed that he saw it working (so it was not my illusion). Let's just say the scrimba platform has some unknown differences than a normal static website, possibly caused by some complexity their system uses (for example, you will notice many requests/responses under the hood when you enable your browser's debug window).

Regards,
Ray Luo

Olof Paulson

unread,
Jun 8, 2021, 10:24:39 AM6/8/21
to brython
Hey Guys!
Sorry for late response...
@Andy and @Ray I am sorry for being unclear...
I haven’t really been able to run either of your scrims as expected...

When I OPEN them via the URL, i.e first access - they displayed charts- BUT...
Andys Chart
was ’super frozen’ i.e. It was really small in corner, and I couldn’t change anything, and if I tried to execute the code. it failed.

Rays CSV,
 similar thing, when I loaded the scrim, I saw the graph and as far as the code was concerned, the read of the data came from the CSV...BUT 
when I tried to run the scrim, It failed and woudl not read from the CSV...That’s what I meant by saying ’it worked on your end’, and ’...an artefact on your end’

My exclamation to Ray about getting Scrimba to work with CSV was derived from seeing the chart based on CSV...When it subsequently failed for me..I guessed it was me not understanding some obvious thing or having some wrong version of something installed.
I thought Maybe Scrimba had changed something, but seems they haven’t

So I am apologize, but have to report I actually haven’t gotten either of the two to work my end, but have managed to execute a linechart(Covid R’s), and as of yesterday a simple bar/column chart to work for css.py( frequency plot)

------ My mental status/plan as of now---
- Check out how to re-use @Andys access to a separate .py file by using import
- try to get feedback from you guys on other ways to access data from another .py file or maybe online service/ URL ( in Scrimba)
- Try to plot pie charts, and some others in css...
- try to move forward with testing Brycharts, and see if ?Piechart? I originally tried a few weeks ago still works 
- Ask Scrimba if they can see some reason why Brython 3.9 may be crashing, and generally behaving in a different manner
- Examine the ’reality’ of file reading in Scrimba... It makes no sense to me that the index.html file is allowed to call scripts from a ’main.py’ file but is totally unable to do the same for a file called .main.txt, or main.csv...
If there is such a general ’lock’ maybe a way around it would be to translate the call to another file or ??
It seems to me that the problem cannot be impossible to ’hack’ in a neat and reasonably transaprent way...
The fact that I cannot come up with such a way, likely has more to do with my lack of experience in the area, than the impossiblity of the problem ;-)
- Also increasingly annoyed by platfroms such as Scrimba..jfiddle and the ’rest of them’ not really working properly at all on mobile/ ipad devices...
I am guessing it is something to do with resources missing or something, rather than the ’specs’ of the hardware...but again I have next to zero knowledge on the subject...

For file reading, I still find @Pierres workaround for getting the files very ...very sexy..if only I can strip it away from ’view’ i.e move it somewhere where the users isn’t likely to see it...
-Would it be possible to put it in the .html file? how would it then be called from the code...in my .py file...

Any suggestions and help very appreciated, and I am so happy and thankful for your help sofar.

Here is my latest in ’Olof plays with graphs - Die roll frequency’ 
https://scrimba.com/scrim/cavWEBCw

Ray Luo

unread,
Jun 8, 2021, 6:00:55 PM6/8/21
to brython
The index.html is able to "call" main.py, that is achieved by HTML's <script> tag. That is indeed different than our Python code attempting open("sample.txt") or open("sample.csv") via AJAX. Olof, you may do some research on Scrimba to see whether their plethora of javascript courses actually demonstrate how AJAX can be done on Scrimba. Or, alternatively, if Pierre can provide a proof-of-concept javascript snippet to mimic how Brython would open(...) a file, we can go from there to see whether that snippet can be run inside Scrimba.

Regarding to MockFile, it is supposed to also work when being put inside its own file mockfile.py, and then in your main.py you can do "from mockfile import open". Olof you can try that on your local machine and then on Scrimba. If they behave differently, that would go back to your "Ask Scrimba if they can see some reason why Brython 3.9 may be crashing, and generally behaving in a different manner" topic. Currently, Olof, your Python course is among the most popular courses on Scrimba. They should treat you as a VIP. No? :-)

And congrats to your frequency chart.
Reply all
Reply to author
Forward
0 new messages