rd4com
rd4com
MModular
Created by Jack Clayton on 5/11/2024 in #community-showcase
mojo-ui-html: UI, HTML, CSS
Hello, just upgraded mojo-ui-html for 25.1.0 and added a few things! It has a mojoproject.toml and next step is to put it in the community repo. rd4com/mojo-ui-html Please make sure to check the todolist_example on top of the updated readme.md In addition to UI, keyboard can switch focus between widgets and "2D game". Widgets:Window, Collapsible, ScrollableArea, Button, ComboBox, Table, Toggle, Text, Slider, ColorSelector, DateSelector, TimeSelector, RawHtml, ..! Tested it by creating a music making app (stay tuned, daft punk lovers), feedbacks welcome, have fun!
🎮 Good start to learn mojo by creating a small videogame !
def main():
var GUI = Server()
GUI.request_interval_second = 0 #no Time.sleep between events
GUI.keyboard_handler = True
var pos = SIMD[DType.int32, 2](0) #[x, y]
while GUI.NeedNewRendition():
k = GUI.KeyDown()
if not k.isa[NoneType]():
# if k.isa[Int]():
# print(k[Int])# example: ord('a'), ..
if k.isa[String]():
var k_tmp = k[String]
if k_tmp == "ArrowUp": pos[1] -= 10
elif k_tmp == "ArrowDown": pos[1] += 10
elif k_tmp == "ArrowLeft": pos[0] -= 10
elif k_tmp == "ArrowRight": pos[0] += 10
GUI.RawHtml(String(
"<div style='position:absolute;",
"left:",pos[0],";",
"top:", pos[1],";"
"'>🚙</div>"
))
def main():
var GUI = Server()
GUI.request_interval_second = 0 #no Time.sleep between events
GUI.keyboard_handler = True
var pos = SIMD[DType.int32, 2](0) #[x, y]
while GUI.NeedNewRendition():
k = GUI.KeyDown()
if not k.isa[NoneType]():
# if k.isa[Int]():
# print(k[Int])# example: ord('a'), ..
if k.isa[String]():
var k_tmp = k[String]
if k_tmp == "ArrowUp": pos[1] -= 10
elif k_tmp == "ArrowDown": pos[1] += 10
elif k_tmp == "ArrowLeft": pos[0] -= 10
elif k_tmp == "ArrowRight": pos[0] += 10
GUI.RawHtml(String(
"<div style='position:absolute;",
"left:",pos[0],";",
"top:", pos[1],";"
"'>🚙</div>"
))
⬅️ ⬆️ ⬇️ ➡️ to move the 🚙 on the html page!
5 replies
MModular
Created by Ivo Balbaert on 6/14/2024 in #community-showcase
Mojo in the Tiobe-index
yep, it has an even better position now 📈🎉 ! (see february 2025 news) https://www.tiobe.com/tiobe-index/
8 replies
MModular
Created by Jack Clayton on 5/11/2024 in #community-showcase
Lightbug HTTP: Mojo web framework
Could it be a cache thing ?
464 replies
MModular
Created by Jack Clayton on 5/11/2024 in #community-showcase
Lightbug HTTP: Mojo web framework
Hello, good job everybody, there is a small thing that can help a lot for HTML performance: when web browser render a page, it then request the icon for the tab, if the server don't give one, it will request it all the time for all pages, (so all requests from interaction results in multiple ones) in mojo ui html, solved it by creating an inlined default one: example for inlined favicon: https://stackoverflow.com/questions/66935329/use-inline-svg-as-favicon
464 replies
MModular
Created by Jack Clayton on 5/11/2024 in #community-showcase
Lightbug HTTP: Mojo web framework
464 replies
MModular
Created by Jack Clayton on 5/11/2024 in #community-showcase
Lightbug HTTP: Mojo web framework
I think it is possible to add an HTTP header that specify that the cache should expire after a certain amount of time 👍
464 replies
MModular
Created by Jack Clayton on 5/11/2024 in #community-showcase
Lightbug HTTP: Mojo web framework
There is github desktop and sublime merge for easy gui git works if needed
464 replies
MModular
Created by Jack Clayton on 5/11/2024 in #community-showcase
Lightbug HTTP: Mojo web framework
Hello, simple checkbox with emoji's if needed:
var tmp_autoplay = "✅ AutoPlay" if PlaySoundsSequence else "⬜ AutoPlay"
var tmp_autoplay = "✅ AutoPlay" if PlaySoundsSequence else "⬜ AutoPlay"
(ping to @carlcaulkett)
464 replies
MModular
Created by rd4com on 10/18/2024 in #community-showcase
:hatching_chick: mojo-new-web-framework
Hello Carl! they are differents (for differents uses) and will exist on separate repos 👍 For example ui-html is easy and can be just "buttons a loop" but is for a single user app, the other (this one) is more for classes ("components") to compose larger things but is for multiple separate users, each users have a live connection (with websockets) and their own states (instances of structs). It is a little bit like a web framework with the states on the server. I'd like to keep ui-html self contained with zero dependencies so people can do anything with it, for the other one, not sure yet but i thought about it too (🥳 lightbug backend) 👍 One of the big idea for what i'd like it to be, is to move from PythonObject to Object eventually !
10 replies
MModular
Created by Tyler Hillery on 12/4/2024 in #questions
1 Billion nested loop iterations Mojo Implementation - seeking feedback
There is also the Advent of Code in mojo if you'd like to continue 👍 #advent-of-code-2024
67 replies
MModular
Created by Manuel Saelices on 11/28/2024 in #questions
Passing method references to high-order functions
Hello @Manuel Saelices, here is an example with parameters:
@value
struct Foo:
fn myfunc(self) -> Int:
return 42

# Note: capturing [_] for lifetimes/origin !
fn high_order_func[f: fn () capturing [_] -> Int]() -> Int:
return f()

fn main():
foo = Foo()

@parameter
fn foo_myfunc() -> Int:
return foo.myfunc()

#__type_of(foo).__del__(foo^)
print(high_order_func[foo_myfunc]())
@value
struct Foo:
fn myfunc(self) -> Int:
return 42

# Note: capturing [_] for lifetimes/origin !
fn high_order_func[f: fn () capturing [_] -> Int]() -> Int:
return f()

fn main():
foo = Foo()

@parameter
fn foo_myfunc() -> Int:
return foo.myfunc()

#__type_of(foo).__del__(foo^)
print(high_order_func[foo_myfunc]())
8 replies
MModular
Created by christian_drake on 12/4/2024 in #questions
Help I have skill issues
Hello, here is an additional way:
from random import random_ui64, seed
def main():
seed()
for _ in range(10):
var x: UInt8 = random_ui64(0, 255).cast[DType.uint8]()
print(x)
from random import random_ui64, seed
def main():
seed()
for _ in range(10):
var x: UInt8 = random_ui64(0, 255).cast[DType.uint8]()
print(x)
Note: Documentation: random for more functions (rand and randn for different distributions (normal/uniform), for example)
22 replies
MModular
Created by rd4com on 10/18/2024 in #community-showcase
:hatching_chick: mojo-new-web-framework
Yes that's an amazing great idea !
10 replies
MModular
Created by rd4com on 10/18/2024 in #community-showcase
:hatching_chick: mojo-new-web-framework
No description
10 replies
MModular
Created by aurelian on 10/19/2024 in #questions
why is this return a copy?
There is the manual (which is really good ❤️‍🔥): https://docs.modular.com/mojo/manual/values/lifetimes
39 replies
MModular
Created by aurelian on 10/19/2024 in #questions
why is this return a copy?
@aurelian , there is a video that explains this: https://www.youtube.com/watch?v=9ag0fPMmYPQ&ab_channel=Modular
39 replies
MModular
Created by rd4com on 10/18/2024 in #community-showcase
:hatching_chick: mojo-new-web-framework
The idea is: "The back-end and the front-end in the same structs!" It works with websockets, a Dict is on the server for visitors states, that way, less complixity 👍 Doing it that way removes the need for creating rest API, events are sent trough websockets, which is the session, and server send a dom-tree as json then the browser just render it.
10 replies
MModular
Created by jmky on 10/7/2024 in #questions
Is there a disdain for Rust from the Mojo team?
It would not make sense to move lifetimes out of the parameter world, because things can be parametrized on lifetime, and lifetimes can be parametrized. As a superset of python, mojo have theses [] for generics(parametrization), that's all there is to it really 👍
23 replies
MModular
Created by jmky on 10/7/2024 in #questions
Is there a disdain for Rust from the Mojo team?
# print
def main():
print("hello world")

# create variable (not learned types yet)
def main2():
a = "hello world"
b = 1
print(a, b)

# print trough function
def printer(a):
print(a)
def main3():
a = "hello world"
printer(a)

# modify variables
def add_one(inout a: Int):
a+=1
def main4():
a = 0
add_one(a)
print(a)

# here there is another gradual step (currently worked on)
# (create auto dereferenced reference and not learned lifetime yet)

# create safe reference (not learned lifetime yet)
def main5():
a = 0
b = Pointer.address_of(a)
a+=1
print(b[])

# return auto dereference (not learned lifetime yet)
def return_ref(arg: List[Int])->ref[arg]List[Int]:
return arg

def main6():
a = List(1,2,3)
a.append(4)
print(return_ref(a)[3])

# return mutable auto dereference (not learned lifetime yet)
def return_mut_ref(inout arg: List[Int])->ref[arg]List[Int]:
return arg

def main7():
a = List(1,2,3)
return_mut_ref(a).append(4)
print(len(a))


# return inferred mutability auto dereference (not learned lifetime yet)
def return_inferred_mut_ref(ref[_]arg: List[Int])->ref[arg]List[Int]:
return arg

def main8():
a = List(1,2,3)
return_mut_ref(a).append(4)
print(len(a))

# return immutable auto dereference
def return_immutable_ref[L:ImmutableLifetime](ref[L]arg: List[Int])->ref[L]List[Int]:
return arg

def main9():
a = List(1,2,3)
a.append(4)
print(return_immutable_ref(a)[4])
print(len(a))


# return mutable reference
def return_mutable_ref2[L:MutableLifetime](ref[L]arg: List[Int])->Pointer[List[Int],L]:
return Pointer.address_of(arg)

def main10():
a = List(1,2,3)
b = return_mutable_ref2(a)
c = b
c[].append(4)
print(len(a))
# print
def main():
print("hello world")

# create variable (not learned types yet)
def main2():
a = "hello world"
b = 1
print(a, b)

# print trough function
def printer(a):
print(a)
def main3():
a = "hello world"
printer(a)

# modify variables
def add_one(inout a: Int):
a+=1
def main4():
a = 0
add_one(a)
print(a)

# here there is another gradual step (currently worked on)
# (create auto dereferenced reference and not learned lifetime yet)

# create safe reference (not learned lifetime yet)
def main5():
a = 0
b = Pointer.address_of(a)
a+=1
print(b[])

# return auto dereference (not learned lifetime yet)
def return_ref(arg: List[Int])->ref[arg]List[Int]:
return arg

def main6():
a = List(1,2,3)
a.append(4)
print(return_ref(a)[3])

# return mutable auto dereference (not learned lifetime yet)
def return_mut_ref(inout arg: List[Int])->ref[arg]List[Int]:
return arg

def main7():
a = List(1,2,3)
return_mut_ref(a).append(4)
print(len(a))


# return inferred mutability auto dereference (not learned lifetime yet)
def return_inferred_mut_ref(ref[_]arg: List[Int])->ref[arg]List[Int]:
return arg

def main8():
a = List(1,2,3)
return_mut_ref(a).append(4)
print(len(a))

# return immutable auto dereference
def return_immutable_ref[L:ImmutableLifetime](ref[L]arg: List[Int])->ref[L]List[Int]:
return arg

def main9():
a = List(1,2,3)
a.append(4)
print(return_immutable_ref(a)[4])
print(len(a))


# return mutable reference
def return_mutable_ref2[L:MutableLifetime](ref[L]arg: List[Int])->Pointer[List[Int],L]:
return Pointer.address_of(arg)

def main10():
a = List(1,2,3)
b = return_mutable_ref2(a)
c = b
c[].append(4)
print(len(a))
23 replies
MModular
Created by jmky on 10/7/2024 in #questions
Is there a disdain for Rust from the Mojo team?
It is just about parameters (you'll see as you read). There is also that thing called progressive exposure to complexity, i like that there is a progressive way of learning as you build/create, and it requires consistent choices that makes sense: (note how lifetimes are parameters, and how lifetimes can also be parametrized on mutability)
23 replies