import a module in the context of github pages.

302 views
Skip to first unread message

Jon Schull

unread,
Apr 21, 2021, 2:16:55 PM4/21/21
to brython

I'm hosting a brython app at github pages.

I have a subdirectory “imports” containing two files

-imports 
--    __init__.py 
--    nodestuff.py

this line:
     from imports.nodestuff import getNode, getEdges, options

works when I serve locally (via python -m http.server 8000)

but it doesnt’ work when I serve from jonschull.github.io
see https://jonschull.github.io/index%20w%20interactions%20editable.html

Traceback (most recent call last): File https://jonschull.github.io/index%20w%20interactions%20editable.html/__main__ line 21, in from imports.nodeStuff import getNodes, getEdges, options ModuleNotFoundError: imports

Thanks for any help

Kiko

unread,
Apr 21, 2021, 3:23:08 PM4/21/21
to bry...@googlegroups.com
Hi Jon,

If you use python imports to import py files within your html code
then you will need a server to serve the requests you do using ajax
(Brython import machinery uses ajax calls). This is not possible if
you use GH pages (static pages, no server to serve your requests).

You could solve this issue in several ways:

- Easier: Include all the python code in the HTML code so you do not
have to import it.
- Maybe not so easy: Transpile your dependencies to JS (see
'Optimisation' section here:
https://brython.info/static_doc/en/import.html).

I hope it helps.

Kind regards.

> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/3085e352-97f8-478e-9319-3f8c1ff12b8fn%40googlegroups.com.
>

Ray Luo

unread,
Apr 22, 2021, 5:05:19 PM4/22/21
to brython
If it works locally, it should also work when deploying to github pages. Github pages is merely a static html hosting site (from the perspective of your front-end codes, at least).

However, I have a theory that, because Github Pages supports 2 major types of sites: user (or organization) site and project site, such a design inadvertently has a collision in the namespace, so your this url:


could be interpreted by Github backend to think you want to visit a page for your project "imports". You do not have such a project, so that URL ends up with an 404 Not Found.

I would suggest you host your website as a project site. That type always works well. You can refer to my example here: https://rayluo.github.io/brython-project-template/   You can click the "Fork me" banner at upper right to see its source code.

By the way, Jon, you have a typo on this line, too. The "nodeStuff" should be "nodestuff". https://github.com/jonschull/jonschull.github.io/blob/main/index%20w%20interactions%20editable.html#L53


Regards,
Ray Luo

Abhiraam Eranti

unread,
May 3, 2021, 11:46:30 AM5/3/21
to brython
If i were you i wouldn't use github pages for brython; use firebase, because it is suited for dynamic applications. 

Edward Elliott

unread,
May 3, 2021, 12:14:40 PM5/3/21
to bry...@googlegroups.com
What are "dynamic" applications in this context?  Every application is static in some regards and dynamic in others.

What particular advantages does firebase offer?  I browsed the website, all I see is a lot of marketing fluff.  Why would you tie yourself to Google just to run a web app?  What can you do on firebase that you can't do on github?

As best I can tell, firebase gives you 1) a javascript SDK, which is useless in brython and 2) access to CDNs for distributed hosting.  Unless you're running a global-scale ultra-high-performance web app, or paying ridiculous data fees to your hosting provider, I don't see the point.  Wringing a few hundred milliseconds of HTTP roundtrip time from a local CDN is meaningless for all but the most performance sensitive apps.  If you're that performance sensitive, you're not using brython anyway.

That's my quick read.  But maybe I'm missing something.  Can someone please tell us how using firebase would help a brython app?


Abhiraam Eranti

unread,
May 3, 2021, 12:20:37 PM5/3/21
to bry...@googlegroups.com
firebase is suited for dynamic applications, while github pages is tailored for static sites only. If won't really work if you require links/routers and if your app is more than one page; My app didn't work on github pages because of its use of query string and client-side routing, but it worked on firebase. You don't have to use firebase, but it's honestly pretty easy to set up.

Edward Elliott

unread,
May 3, 2021, 1:46:19 PM5/3/21
to bry...@googlegroups.com
Sorry but I don't see any new info here.  All you did was restate the same things.  Perhaps a concrete example would help clarify what you're trying to do.

All applications are dynamic applications.  It's not a standardized term, google turns up many conflicting definitions.  What do you mean by it?

Github is perfectly capable of serving brython scripts.  You just have to link to the right url.  Say you want git.py.  By default github serves an html page with the brython interface:
You just have to request the raw file instead:

And invoke brython with the right paths to import your modules:

brython ( { pythonpath : modulepaths } );


Now you can run all your scripts from github source.  Not sure you'd want to, but it's easily doable.

Query strings are also no problem.  Just add a query string to your raw script url and github still returns the file:
So query string is still available for processing by your app.

Client side routing is also no problem.  You just have to structure your urls correctly to return the raw files you want.  Perhaps firebase is also intercepting and disabling URL requests from bubbling up to browser dispatch?  That's easy in brython, just call Event.preventDefault in a listener for link click events.

I'm not against firebase if I understand the purpose of it.  So far I don't see the purpose.  Brython + github can do all the things you mentioned.


Message has been deleted
Message has been deleted

Abhiraam Eranti

unread,
May 3, 2021, 5:41:43 PM5/3/21
to brython
ok then, that's great. 

So could you kind of elaborate on the Event.preventdefault part? 

And also i kind of think that it's just more convenient to use firebase because its kind of built with all these handlers that you specify, but its really up to you.

Abhiraam Eranti

unread,
May 3, 2021, 5:46:45 PM5/3/21
to bry...@googlegroups.com
also would like to add that using the raw github links instead of relative imports makes development harder. While yes, you could change it when making commits to github, it all seems really tedious. There are also other services besides firebase for hosting like aws, heroku, render, etc.

Kiko

unread,
May 4, 2021, 4:20:40 AM5/4/21
to bry...@googlegroups.com
2021-05-03 23:46 GMT+02:00, Abhiraam Eranti <abhira...@gmail.com>:
> also would like to add that using the raw github links instead of relative
> imports makes development harder. While yes, you could change it when
> making commits to github, it all seems really tedious. There are also other
> services besides firebase for hosting like aws, heroku, render, etc.

You could use:

> brython-cli --modules

to create your own distribution and 'convert' your py files to js
files that are loaded with the initial request. This way you do not
need to import py files [1].

The py import is done using AJAX requests and to do so you need a
server that is responding to the requests. In GH-pages you do not have
the server part so it is better to load everything when the web page
is loaded.

Tomorrow I will publish a new post [2] (in spanish) detailing how to
do so in case you want to have a look.

[1] https://brython.info/static_doc/en/import.html
[2] pybonacci.org
>>> <https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault>
>>> in
>>> a listener for link click events.
>>>
>>> I'm not against firebase if I understand the purpose of it. So far I
>>> don't see the purpose. Brython + github can do all the things you
>>> mentioned.
>>>
>>>
>>> On Mon, May 3, 2021 at 6:20 PM Abhiraam Eranti <abhira...@gmail.com>
>>> wrote:
>>>
>>>> firebase is suited for dynamic applications, while github pages is
>>>> tailored for static sites only. If won't really work if you require
>>>> links/routers and if your app is more than one page; My app didn't work
>>>> on
>>>> github pages because of its use of query string and client-side
>>>> routing,
>>>> but it worked on firebase. You don't have to use firebase, but it's
>>>> honestly pretty easy to set up.
>>>>
>>>> On Mon, May 3, 2021 at 9:14 AM Edward Elliott <ese...@gmail.com> wrote:
>>>>
>>>>> What are "dynamic" applications in this context? Every application is
>>>>> static in some regards and dynamic in others.
>>>>>
>>>>> What particular advantages does firebase offer? I browsed the website
>>>>> <https://firebase.google.com/>, all I see is a lot of marketing
>>>>> fluff. Why would you tie yourself to Google just to run a web app?
>>>>> What
>>>>> can you do on firebase that you can't do on github?
>>>>>
>>>>> As best I can tell <https://firebase.google.com/docs/web/learn-more>,
>>>>>> <https://groups.google.com/d/msgid/brython/d357cacf-8bdd-4397-bd34-62e5b8a16421n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "brython" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to brython+u...@googlegroups.com.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/brython/CAF1%3DpDmEXyUOkK4cUci%2B8dxeFoD5toLyXRLPF%3DLrDTY4WSNeDw%40mail.gmail.com
>>>>> <https://groups.google.com/d/msgid/brython/CAF1%3DpDmEXyUOkK4cUci%2B8dxeFoD5toLyXRLPF%3DLrDTY4WSNeDw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "brython" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to brython+u...@googlegroups.com.
>>>>
>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/brython/CAGTurTUz15dJu8pRwHcN7ndPOGXxdUDDWTYF7HCnwREmfMmv7A%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/brython/CAGTurTUz15dJu8pRwHcN7ndPOGXxdUDDWTYF7HCnwREmfMmv7A%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "brython" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to brython+u...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/brython/6b0938da-1551-43de-bb22-6a5594dcf1abn%40googlegroups.com
>> <https://groups.google.com/d/msgid/brython/6b0938da-1551-43de-bb22-6a5594dcf1abn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/CAGTurTXy3qkTfPvHrM22QP35qJRcBvk_MV_%2B-rPYpTdQdY2q0g%40mail.gmail.com.
>

Abhiraam Eranti

unread,
May 5, 2021, 12:55:34 AM5/5/21
to brython
huh. that's great, then. I don't speak Spanish, but will definitely see your tutorial(with a translator)

Kiko

unread,
May 5, 2021, 2:06:21 AM5/5/21
to bry...@googlegroups.com
2021-05-05 6:55 GMT+02:00, Abhiraam Eranti <abhira...@gmail.com>:
> huh. that's great, then. I don't speak Spanish, but will definitely see
> your tutorial(with a translator)

It's published here:
https://pybonacci.org/2021/05/05/silabacion-de-palabras-con-python-y-como-servicio-web/
> https://groups.google.com/d/msgid/brython/ad266741-e21d-4985-bbec-cd3f7dfcc79an%40googlegroups.com.
>

dgront

unread,
May 5, 2021, 3:51:26 AM5/5/21
to bry...@googlegroups.com

Kiko

unread,
May 5, 2021, 4:57:14 AM5/5/21
to bry...@googlegroups.com
2021-05-05 9:51 GMT+02:00, dgront <dgr...@gmail.com>:
>>
>> It's published here:
>>
>> https://pybonacci.org/2021/05/05/silabacion-de-palabras-con-python-y-como-servicio-web/
>>
> Nice tutorial!

Thanks.

Sorry if it is in spanish. In english you already have a lot of
materials. If anyone has questions or something is not clear from an
automatic translation, please, feel free to ask here or in the
comments of the post.

Best.
>> https://groups.google.com/d/msgid/brython/CAB-sx6131r4pTK_oG1CdX1YVN3f9g6qmAxhMgeSVtB9W4E%3DCuA%40mail.gmail.com
>> .
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/CAKKq%3DURp5A6UE1f9JGHpCCpKZXybMoDWhQ10R%3DXnmMJGzigpfQ%40mail.gmail.com.
>

dgront

unread,
May 5, 2021, 5:12:38 AM5/5/21
to bry...@googlegroups.com
Actually I have another question, quite related to the topic of distributing my Brython library, already in the .js form:

Has anyone established CDN location for their package and could share a short info how to do it?

Best,
Dominik

Kiko

unread,
May 5, 2021, 6:33:37 AM5/5/21
to bry...@googlegroups.com
2021-05-05 11:12 GMT+02:00, dgront <dgr...@gmail.com>:
> Actually I have another question, quite related to the topic of
> distributing my Brython library, already in the .js form:
>
> Has anyone established CDN location for their package and could share a
> short info how to do it?

Hi Dominik, I opened a new thread for your question.
>> https://groups.google.com/d/msgid/brython/CAB-sx63ZPD1reG-1rCVQ%3D4qMnWn5vXWzyQ0QCxvsSAYaOvjidQ%40mail.gmail.com
>> .
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/CAKKq%3DUS_jT_svqZRvFDO94%3DymJ3X_qLKC0Jp1Loy0dZK7B7aFQ%40mail.gmail.com.
>

Abhiraam Eranti

unread,
May 5, 2021, 11:46:40 AM5/5/21
to brython
that's fine, i have a translator. Plus, i'm currently learning Spanish

Edward Elliott

unread,
May 8, 2021, 9:12:38 AM5/8/21
to bry...@googlegroups.com
If you want to use firebase for hosting brython apps, you certainly can.  I don't dispute that it works.  I simply question whether it's a good idea.  Firebase does lots of complex stuff behind the scenes that is unnecessary and inapplicable to brython.  And being from Google, it's likely collecting a lot of data on your users for Google's purposes.

Philosophically, I prefer simple tools that focus on one thing and do it well.  If you prefer to throw everything and the kitchen sink at a problem, that's your choice.  It's not worth arguing over here.

What is worth discussing are your incorrect claims about brython web apps.  Others may read your claims above and misunderstand how brython web apps work.  So to clarify:

1) Brython web apps can be served directly from github.  It takes a bit of fiddling and is probably not a good idea, but it is doable.  See my post Serving files directly from github for more info.

2) Normal brython imports work perfectly fine this way.  You don't need to provide full urls or include <script> tags in your html source.  Just pass the proper search path when you invoke brython () and use "import foo" in your brython script as usual.  There's nothing tedious about it.

3) Client side routing is not magic.  It simply means your app handles routing requests instead of passing them to the browser.  You just intercept click events on links and handle them within your app (ajax the new content and update the page) rather than through the browser.  One part is registering handlers for click events.  Another part is cancelling browser navigation.  Event.preventDefault () addresses the second part.  If you want more info, there are plenty of online javascript event tutorials.  It's not specific to brython and not worth discussing here.

It sounds like firebase provides some sort of library that handles client side routing for you.  That's fine.  But it's also easy to implement yourself.  Brython is perfectly capable of client side routing without any fancy frameworks.

HTH,


Abhiraam Eranti

unread,
May 8, 2021, 11:54:06 AM5/8/21
to bry...@googlegroups.com
Yeah, ik that's what I'm saying. It's certainly possible to host a brython site on github pages, though there are some inconveniences. Also it's not just firebase that you can use as an alternative, but others instead. Netlify, aws, etc can work too

Jon Schull

unread,
May 11, 2021, 11:57:44 PM5/11/21
to brython
Very helpful.  I'm hopful.  Thanks!
Reply all
Reply to author
Forward
0 new messages